feat(files): add folder download as zip
- Add zip folder API endpoint - Add download button to folder rows - Enforce max archive size limit - Document endpoint in OpenAPI
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestZipDirectory(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := t.TempDir()
|
||||
sub := filepath.Join(root, "nested")
|
||||
if err := os.Mkdir(sub, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "a.txt"), []byte("hello"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(sub, "b.txt"), []byte("world"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
zipPath, err := ZipDirectory(root, 1<<20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(zipPath)
|
||||
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.Close()
|
||||
names := make(map[string]string)
|
||||
for _, f := range r.File {
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(rc)
|
||||
_ = rc.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
names[f.Name] = string(body)
|
||||
}
|
||||
if names["a.txt"] != "hello" || names["nested/b.txt"] != "world" {
|
||||
t.Fatalf("unexpected zip contents: %#v", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZipArchiveName(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := filepath.Join("Users", "dan", "Projects")
|
||||
if got := ZipArchiveName(dir); got != "Projects.zip" {
|
||||
t.Fatalf("ZipArchiveName() = %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user