Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/src/Application/File/UploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* POST /files 的上传主流程:限长复制 → 内容嗅探 → 扩展名+MIME 双白名单 →
* 幂等预检 → Flysystem 落盘 → sand_system_file 元数据、用途登记与幂等记录
* 幂等预检 → SafeFs 原子落盘 → sand_system_file 元数据、用途登记与幂等记录
* 同一事务入库。幂等重放经 IdempotentReplay 抛给 FileHttp 转成功信封。
*/
final class UploadService
Expand Down
47 changes: 21 additions & 26 deletions server/src/Infra/File/LocalFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

namespace Sandadmin\Infra\File;

use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Sandadmin\Domain\File\FileException;

/**
* 1.0 只接本机磁盘,直接用 Flysystem Local adapter(§3.8.4/§4.3,不再自定义存储接口)。
* 1.0 只接本机磁盘,一切写/删/判存都经 SafeFs 收口(M-03):
* 根内限定 + lstat 拒软链 + 原子写 + 显式权限位。
* 对象键是随机生成的 storage_key,原始文件名只写 sand_system_file 元数据;
* 存储根在 web 根之外(config/file.php 默认 runtime/files)。
*/
Expand All @@ -22,7 +20,7 @@ final class LocalFileStorage
public const KEY_PATTERN = '/\A[a-f0-9]{32}\z/';

private function __construct(
private readonly Filesystem $filesystem,
private readonly SafeFs $safe,
private readonly string $root,
) {
}
Expand All @@ -32,25 +30,22 @@ public static function open(string $root): self
if ($root === '') {
throw FileException::internal('存储配置无效');
}
if (!is_dir($root) && !mkdir($root, 0700, true) && !is_dir($root)) {
throw FileException::internal('无法创建存储目录');
try {
$safe = SafeFs::open($root);
} catch (SafeFsException $error) {
throw FileException::internal('存储目录不可用', $error);
}
$resolved = realpath($root);
if ($resolved === false || !is_dir($resolved) || !is_writable($resolved)) {
$resolved = $safe->root();
if (!is_writable($resolved)) {
throw FileException::internal('存储目录不可用');
}
$adapter = new LocalFilesystemAdapter(
$resolved,
null,
LOCK_EX,
LocalFilesystemAdapter::DISALLOW_LINKS,
);

return new self(new Filesystem($adapter), $resolved);

return new self($safe, $resolved);
}

/**
* 流式写入并返回随机对象键;键冲突时重试。
* 写盘经 SafeFs 原子写:同目录临时文件 + rename 落位,权限位显式 0640。
*
* @param resource $stream
*/
Expand All @@ -64,8 +59,8 @@ public function writeStream(mixed $stream): string
}
try {
$key = $this->allocateKey();
$this->filesystem->writeStream($key, $stream);
} catch (FilesystemException $error) {
$this->safe->writeStream($key, $stream, 0640);
} catch (SafeFsException $error) {
throw FileException::internal('文件写入失败', $error);
}

Expand All @@ -76,8 +71,8 @@ public function exists(string $key): bool
{
self::assertKey($key);
try {
return $this->filesystem->fileExists($key);
} catch (FilesystemException $error) {
return $this->safe->kind($key) === 'file';
} catch (SafeFsException $error) {
throw FileException::internal('无法读取存储', $error);
}
}
Expand All @@ -86,16 +81,16 @@ public function delete(string $key): void
{
self::assertKey($key);
try {
if (!$this->filesystem->fileExists($key)) {
if ($this->safe->kind($key) === null) {
return;
}
$this->filesystem->delete($key);
} catch (FilesystemException $error) {
$this->safe->removeFile($key);
} catch (SafeFsException $error) {
throw FileException::internal('文件删除失败', $error);
}
}

/** 本机适配器下对象键即根目录内相对路径;仅下载响应使用。 */
/** 本机存储下对象键即根目录内相对路径;仅下载响应使用。 */
public function localPath(string $key): string
{
self::assertKey($key);
Expand All @@ -114,7 +109,7 @@ private function allocateKey(): string
{
for ($attempt = 0; $attempt < 3; $attempt++) {
$key = bin2hex(random_bytes(16));
if (!$this->filesystem->fileExists($key)) {
if ($this->safe->kind($key) === null) {
return $key;
}
}
Expand Down
Loading
Loading