feat(dashboard): traverse file URI from file ID (#2412)
This commit is contained in:
@@ -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"))
|
||||
}
|
||||
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -55,6 +55,8 @@ type (
|
||||
PatchMedata(ctx context.Context, path []*fs.URI, data ...fs.MetadataPatch) error
|
||||
// CreateViewerSession creates a viewer session for given file
|
||||
CreateViewerSession(ctx context.Context, uri *fs.URI, version string, viewer *setting.Viewer) (*ViewerSession, error)
|
||||
// TraverseFile traverses a file to its root file, return the file with linked root.
|
||||
TraverseFile(ctx context.Context, fileID int) (fs.File, error)
|
||||
}
|
||||
|
||||
FsManagement interface {
|
||||
|
||||
@@ -277,6 +277,10 @@ func (l *manager) CreateOrUpdateShare(ctx context.Context, path *fs.URI, args *C
|
||||
return share, nil
|
||||
}
|
||||
|
||||
func (m *manager) TraverseFile(ctx context.Context, fileID int) (fs.File, error) {
|
||||
return m.fs.TraverseFile(ctx, fileID)
|
||||
}
|
||||
|
||||
func getEntityDisplayName(f fs.File, e fs.Entity) string {
|
||||
switch e.Type() {
|
||||
case types.EntityTypeThumbnail:
|
||||
|
||||
Reference in New Issue
Block a user