[server][core][trash] Return size of restored resources

This commit is contained in:
Abhishek Shroff
2025-03-17 23:03:39 +05:30
parent b75b7e6f30
commit 2c4541354b
5 changed files with 30 additions and 27 deletions

View File

@@ -9,13 +9,16 @@ import (
)
func formatRow(id string, dir bool, size int, sha256, name, permissions string) string {
sizeStr := " -"
if dir {
sha256 = "dir "
sha256 = "- "
} else {
sizeStr = FormatSize(size)
}
if sha256 == "" {
sha256 = "xxxxxxxx"
}
return fmt.Sprintf("%s %4s %s %-24s %s", id, FormatSize(size), sha256[0:8], name, permissions)
return fmt.Sprintf("%s %s %s %-24s %s", id, sizeStr, sha256[0:8], name, permissions)
}
func FormatResourceSummaryWithName(r fs.Resource, name string) string {
@@ -31,11 +34,11 @@ func FormatResourceSummary(r fs.Resource) string {
}
func FormatSize(size int) string {
suffix := []string{"", "K", "M", "G", "T"}
suffix := []string{"B", "K", "M", "G", "T"}
si := 0
for ; size >= 1000 && si < len(suffix); si, size = si+1, size/1000 {
}
return fmt.Sprintf("%d%s", size, suffix[si])
return fmt.Sprintf("%4d%s", size, suffix[si])
}
func formatGrantsJson(j []byte) string {

View File

@@ -24,7 +24,7 @@ func setupRestoreCommand() *cobra.Command {
}
}
p, err := cmd.Flags().GetString("parent")
r, items, err := r.Restore(p, "", true)
r, items, size, err := r.Restore(p, "", true)
if err != nil {
fmt.Println("cannot restore '" + uuid + "': " + err.Error())
os.Exit(1)
@@ -35,11 +35,7 @@ func setupRestoreCommand() *cobra.Command {
fmt.Println("cannot get path: " + err.Error())
os.Exit(1)
}
if r.Dir() {
fmt.Printf("Restored dir: %s (%d items)\n", path, items)
} else {
fmt.Printf("Restored file: %s\n", path)
}
fmt.Printf("Restored %s (%d items, %s)\n", path, items, common.FormatSize(int(size)))
}
},
}

View File

@@ -80,7 +80,7 @@ UPDATE resources
SET modified = NOW(), deleted = NULL
FROM nodes
WHERE resources.id = nodes.id
RETURNING 1
RETURNING resources.content_length
`
type RestoreDeletedParams struct {
@@ -88,19 +88,19 @@ type RestoreDeletedParams struct {
Username pgtype.Text
}
func (q *Queries) RestoreDeleted(ctx context.Context, arg RestoreDeletedParams) ([]int32, error) {
func (q *Queries) RestoreDeleted(ctx context.Context, arg RestoreDeletedParams) ([]int64, error) {
rows, err := q.db.Query(ctx, restoreDeleted, arg.ResourceID, arg.Username)
if err != nil {
return nil, err
}
defer rows.Close()
var items []int32
var items []int64
for rows.Next() {
var column_1 int32
if err := rows.Scan(&column_1); err != nil {
var content_length int64
if err := rows.Scan(&content_length); err != nil {
return nil, err
}
items = append(items, column_1)
items = append(items, content_length)
}
if err := rows.Err(); err != nil {
return nil, err

View File

@@ -80,9 +80,8 @@ func (f filesystem) TrashSummary() (int, int, error) {
// - Parent must not be deleted
// - Parent must have write permission
// - No name conflict with exiting resource
func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool) (Resource, int, error) {
func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool) (res Resource, count int, size int64, err error) {
var p Resource
var err error
if parentPathOrUUID == "" {
p, err = r.f.ResourceByID(*r.parentID)
} else {
@@ -92,14 +91,16 @@ func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool)
err = ErrParentNotFound
}
if err != nil {
return Resource{}, 0, err
return
}
if p.deleted != nil {
return Resource{}, 0, ErrParentDeleted
err = ErrParentDeleted
return
}
if !p.hasPermission(PermissionWrite) {
return Resource{}, 0, ErrInsufficientPermissions
err = ErrInsufficientPermissions
return
}
if name == "" {
@@ -123,7 +124,7 @@ func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool)
if err == nil {
err = ErrResourceNameConflict
}
return Resource{}, 0, err
return
}
var username pgtype.Text
@@ -131,7 +132,6 @@ func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool)
username.String = r.f.username
username.Valid = true
}
var count int
err = r.f.db.WithTx(r.f.ctx, func(dbh *db.DbHandler) error {
f := r.f.withDb(dbh)
if p.id != *r.parentID || r.name != name {
@@ -145,13 +145,17 @@ func (r Resource) Restore(parentPathOrUUID string, name string, autoRename bool)
r.parentID = update.Parent
}
}
if res, err := f.db.RestoreDeleted(f.ctx, db.RestoreDeletedParams{Username: username, ResourceID: r.id}); err != nil {
if del, err := f.db.RestoreDeleted(f.ctx, db.RestoreDeletedParams{Username: username, ResourceID: r.id}); err != nil {
return err
} else {
count = len(res)
count = len(del)
for _, l := range del {
size += l
}
}
return f.db.RecomputePermissions(f.ctx, r.ID())
})
return r, count, err
res = r
return
}

View File

@@ -35,7 +35,7 @@ UPDATE resources
SET modified = NOW(), deleted = NULL
FROM nodes
WHERE resources.id = nodes.id
RETURNING 1;
RETURNING resources.content_length;
SELECT r.*
FROM nodes n