Skip to content

Commit

Permalink
feat: add ModTime field to file-related requests and update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bhunter234 committed Jan 8, 2025
1 parent bfedb4d commit e6a6585
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
8 changes: 5 additions & 3 deletions backend/teldrive/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type CreateFileRequest struct {
Encrypted bool `json:"encrypted,omitempty"`
Parts []FilePart `json:"parts,omitempty"`
ParentId string `json:"parentId,omitempty"`
ModTime time.Time `json:"updatedAt,omitempty"`
}

type MoveFileRequest struct {
Expand All @@ -95,7 +96,7 @@ type DirMove struct {

type UpdateFileInformation struct {
Name string `json:"name,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
ModTime *time.Time `json:"updatedAt,omitempty"`
Parts []FilePart `json:"parts,omitempty"`
Size int64 `json:"size,omitempty"`
UploadId string `json:"uploadId,omitempty"`
Expand All @@ -108,8 +109,9 @@ type RemoveFileRequest struct {
Files []string `json:"ids,omitempty"`
}
type CopyFile struct {
Newname string `json:"newName"`
Destination string `json:"destination"`
Newname string `json:"newName"`
Destination string `json:"destination"`
ModTime time.Time `json:"updatedAt,omitempty"`
}

type Session struct {
Expand Down
27 changes: 17 additions & 10 deletions backend/teldrive/teldrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (f *Fs) String() string {

// Precision of the ModTimes in this Fs
func (f *Fs) Precision() time.Duration {
return fs.ModTimeNotSupported
return time.Second
}

// Hashes returns the supported hash types of the filesystem
Expand Down Expand Up @@ -569,7 +569,7 @@ func (f *Fs) moveTo(ctx context.Context, id, srcLeaf, dstLeaf, srcDirectoryID, d
}

if srcLeaf != dstLeaf {
err := f.updateFileInformation(ctx, &api.UpdateFileInformation{Name: dstLeaf}, id)
err := f.updateFileInformation(ctx, &api.UpdateFileInformation{Name: dstLeaf}, id, true)
if err != nil {
return fmt.Errorf("move: failed rename: %w", err)
}
Expand All @@ -579,12 +579,15 @@ func (f *Fs) moveTo(ctx context.Context, id, srcLeaf, dstLeaf, srcDirectoryID, d
}

// updateFileInformation set's various file attributes most importantly it's name
func (f *Fs) updateFileInformation(ctx context.Context, update *api.UpdateFileInformation, fileId string) (err error) {
func (f *Fs) updateFileInformation(ctx context.Context, update *api.UpdateFileInformation, fileId string, skipUpdate bool) (err error) {
opts := rest.Opts{
Method: "PATCH",
Path: "/api/files/" + fileId,
NoResponse: true,
}
if skipUpdate {
opts.Parameters = url.Values{"skiputs": []string{"1"}}
}

err = f.pacer.Call(func() (bool, error) {
resp, err := f.srv.CallJSON(ctx, &opts, update, nil)
Expand Down Expand Up @@ -616,9 +619,12 @@ func (f *Fs) FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut strin
if err != nil {
return "", false, err
}
if len(files) == 0 || files[0].Type == "file" {
if len(files) == 0 {
return "", false, nil
}
if files[0].Type == "file" {
return "", false, fs.ErrorIsFile
}
return files[0].Id, true, nil
}

Expand Down Expand Up @@ -689,10 +695,10 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
}

payload := &api.UpdateFileInformation{
UpdatedAt: Ptr(modTime.UTC()),
Size: src.Size(),
ParentID: directoryID,
Name: leaf,
ModTime: Ptr(modTime.UTC()),
Size: src.Size(),
ParentID: directoryID,
Name: leaf,
}

if uploadInfo != nil {
Expand Down Expand Up @@ -1057,6 +1063,7 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object,
copy := api.CopyFile{
Newname: dstLeaf,
Destination: directoryID,
ModTime: srcObj.ModTime(ctx).UTC(),
}
var info api.FileInfo
err = f.pacer.Call(func() (bool, error) {
Expand Down Expand Up @@ -1127,9 +1134,9 @@ func (o *Object) Storable() bool {
// SetModTime sets the modification time of the local fs object
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
updateInfo := &api.UpdateFileInformation{
UpdatedAt: Ptr(modTime.UTC()),
ModTime: Ptr(modTime.UTC()),
}
err := o.fs.updateFileInformation(ctx, updateInfo, o.id)
err := o.fs.updateFileInformation(ctx, updateInfo, o.id, false)
if err != nil {
return fmt.Errorf("couldn't update mod time: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions backend/teldrive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func (o *Object) createFile(ctx context.Context, src fs.ObjectInfo, uploadInfo *
Parts: uploadInfo.fileChunks,
ChannelID: uploadInfo.channelID,
Encrypted: uploadInfo.encryptFile,
ModTime: src.ModTime(ctx).UTC(),
}

err := o.fs.pacer.Call(func() (bool, error) {
Expand Down

0 comments on commit e6a6585

Please sign in to comment.