Skip to content

Commit 333caa5

Browse files
committed
feat: show global subdirectory size in properties screen (fixes #301)
1 parent ed69a18 commit 333caa5

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

Localizable.xcstrings

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24876,6 +24876,46 @@
2487624876
}
2487724877
}
2487824878
},
24879+
"Subdirectory size" : {
24880+
"localizations" : {
24881+
"de" : {
24882+
"stringUnit" : {
24883+
"state" : "translated",
24884+
"value" : "Unterverzeichnisgröße"
24885+
}
24886+
},
24887+
"es" : {
24888+
"stringUnit" : {
24889+
"state" : "translated",
24890+
"value" : "Tamaño del subdirectorio"
24891+
}
24892+
},
24893+
"it" : {
24894+
"stringUnit" : {
24895+
"state" : "translated",
24896+
"value" : "Dimensione sottodirectory"
24897+
}
24898+
},
24899+
"nl" : {
24900+
"stringUnit" : {
24901+
"state" : "translated",
24902+
"value" : "Omvang submap"
24903+
}
24904+
},
24905+
"uk" : {
24906+
"stringUnit" : {
24907+
"state" : "translated",
24908+
"value" : "Розмір підкаталогу"
24909+
}
24910+
},
24911+
"zh-Hans" : {
24912+
"stringUnit" : {
24913+
"state" : "translated",
24914+
"value" : "子目录大小"
24915+
}
24916+
}
24917+
}
24918+
},
2487924919
"Subpath" : {
2488024920
"localizations" : {
2488124921
"de" : {

Sushitrain/FileView.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct FileView: View {
2828
@State private var openWithAppURL: URL? = nil
2929
@State private var localPath: String? = nil
3030
@State private var showArchive: Bool = false
31+
@State private var subdirectorySizeBytes: Int64? = nil
3132

3233
@Environment(AppState.self) private var appState
3334
@Environment(\.dismiss) private var dismiss
@@ -60,6 +61,10 @@ struct FileView: View {
6061
Text("File size").badge(Self.formatter.string(fromByteCount: file.size()))
6162
}
6263

64+
if let subDirSize = self.subdirectorySizeBytes, file.isDirectory() {
65+
Text("Subdirectory size").badge(Self.formatter.string(fromByteCount: subDirSize))
66+
}
67+
6368
if let md = file.modifiedAt()?.date(), !file.isSymlink() {
6469
Text("Last modified").badge(md.formatted(date: .abbreviated, time: .shortened))
6570

@@ -291,6 +296,10 @@ struct FileView: View {
291296
self.downloaderSheet()
292297
}
293298

299+
.sheet(isPresented: $showEncryptionSheet) {
300+
EncryptionView(entry: self.file)
301+
}
302+
294303
.toolbar {
295304
// Next/previous buttons
296305
if let selfIndex = selfIndex, let siblings = siblings {
@@ -328,14 +337,14 @@ struct FileView: View {
328337
#endif
329338
}
330339

331-
.sheet(isPresented: $showEncryptionSheet) {
332-
EncryptionView(entry: self.file)
333-
}
334-
335340
.onAppear {
336341
selfIndex = self.siblings?.firstIndex(of: file)
337342
}
338343

344+
.task {
345+
await self.update()
346+
}
347+
339348
.onChange(of: file, initial: true) { _, _ in
340349
self.fullyAvailableOnDevices = nil
341350
self.update()
@@ -347,6 +356,24 @@ struct FileView: View {
347356
}
348357
}
349358

359+
private func update() async {
360+
self.subdirectorySizeBytes = nil
361+
let file = self.file
362+
self.subdirectorySizeBytes = await Task.detached {
363+
if file.isDirectory() {
364+
var size: Int64 = 0
365+
do {
366+
try file.recursiveSize(&size)
367+
return size
368+
}
369+
catch {
370+
Log.warn("could not calculate subdirectory size: \(error.localizedDescription)")
371+
}
372+
}
373+
return nil
374+
}.value
375+
}
376+
350377
@ViewBuilder private func zipButton() -> some View {
351378
Button("Explore archive contents", systemImage: "doc.zipper") {
352379
self.showArchive = true

SushitrainCore/src/entry.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ func (entry *Entry) Size() int64 {
9191
return entry.info.FileSize()
9292
}
9393

94+
func (entry *Entry) RecursiveSize() (int64, error) {
95+
if !entry.IsDirectory() {
96+
return entry.Size(), nil
97+
}
98+
99+
prefix := entry.Path() + "/"
100+
leaves, err := entry.Folder.listEntries(prefix, false, true)
101+
if err != nil {
102+
return 0, err
103+
}
104+
105+
var size int64 = 0
106+
err = walkEntries(entry.Path(), leaves, func(leafPrefix string, leaf *model.TreeEntry) (bool, error) {
107+
size += leaf.Size
108+
return true, nil
109+
})
110+
111+
return size, err
112+
}
113+
94114
func (entry *Entry) IsDeleted() bool {
95115
return entry.info.IsDeleted()
96116
}

0 commit comments

Comments
 (0)