feat(dashboard): traverse file URI from file ID (#2412)

This commit is contained in:
Aaron Liu
2025-05-29 09:44:11 +08:00
parent 65095855c1
commit 51fa9f66a5
9 changed files with 67 additions and 3 deletions

View File

@@ -450,7 +450,7 @@ func (f *DBFS) Get(ctx context.Context, path *fs.URI, opts ...fs.Option) (fs.Fil
return nil, fmt.Errorf("failed to get target file: %w", err)
}
if o.notRoot && target.IsRootFolder() {
if o.notRoot && (target == nil || target.IsRootFolder()) {
return nil, fs.ErrNotSupportedAction.WithError(fmt.Errorf("cannot operate root file"))
}

View File

@@ -641,6 +641,40 @@ func (f *DBFS) GetFileFromDirectLink(ctx context.Context, dl *ent.DirectLink) (f
return file, nil
}
func (f *DBFS) TraverseFile(ctx context.Context, fileID int) (fs.File, error) {
fileModel, err := f.fileClient.GetByID(ctx, fileID)
if err != nil {
return nil, err
}
if fileModel.OwnerID != f.user.ID && !f.user.Edges.Group.Permissions.Enabled(int(types.GroupPermissionIsAdmin)) {
return nil, fs.ErrOwnerOnly.WithError(fmt.Errorf("only file owner can traverse file's uri"))
}
file := newFile(nil, fileModel)
// Traverse to the root file
baseNavigator := newBaseNavigator(f.fileClient, defaultFilter, f.user, f.hasher, f.settingClient.DBFS(ctx))
root, err := baseNavigator.findRoot(ctx, file)
if err != nil {
return nil, fmt.Errorf("failed to find root file: %w", err)
}
rootUri := newMyUri()
if fileModel.OwnerID != f.user.ID {
rootUri = newMyIDUri(hashid.EncodeUserID(f.hasher, fileModel.OwnerID))
}
if root.Name() != inventory.RootFolderName {
rootUri = newTrashUri(root.Name())
}
root.Path[pathIndexRoot] = rootUri
root.Path[pathIndexUser] = rootUri
return file, nil
}
func (f *DBFS) deleteEntity(ctx context.Context, target *File, entityId int) (inventory.StorageDiff, error) {
if target.PrimaryEntityID() == entityId {
return nil, fs.ErrNotSupportedAction.WithError(fmt.Errorf("cannot delete current version"))

View File

@@ -95,6 +95,8 @@ type (
VersionControl(ctx context.Context, path *URI, versionId int, delete bool) error
// GetFileFromDirectLink gets a file from a direct link.
GetFileFromDirectLink(ctx context.Context, dl *ent.DirectLink) (File, error)
// TraverseFile traverses a file to its root file, return the file with linked root.
TraverseFile(ctx context.Context, fileID int) (File, error)
}
UploadManager interface {