1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-09 10:07:17 +02:00

store the original uploaded file name as metadata

This commit is contained in:
Gani Georgiev 2022-12-05 14:28:28 +02:00
parent b8cd686b32
commit 599c542c5a
2 changed files with 21 additions and 2 deletions

View File

@ -133,8 +133,17 @@ func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
// rewind
f.Seek(0, io.SeekStart)
originalName := fh.Filename
if len(originalName) > 255 {
// keep only the first 255 chars as a very rudimentary measure
// to prevent the metadata to grow too big in size
originalName = originalName[:255]
}
opts := &blob.WriterOptions{
ContentType: mt.String(),
Metadata: map[string]string{
"original_filename": fh.Filename,
},
}
w, err := s.bucket.NewWriter(s.ctx, fileKey, opts)

View File

@ -162,14 +162,24 @@ func TestFileSystemUploadMultipart(t *testing.T) {
}
defer fs.Close()
uploadErr := fs.UploadMultipart(fh, "newdir/newkey.txt")
fileKey := "newdir/newkey.txt"
uploadErr := fs.UploadMultipart(fh, fileKey)
if uploadErr != nil {
t.Fatal(uploadErr)
}
if exists, _ := fs.Exists("newdir/newkey.txt"); !exists {
if exists, _ := fs.Exists(fileKey); !exists {
t.Fatalf("Expected newdir/newkey.txt to exist")
}
attrs, err := fs.Attributes(fileKey)
if err != nil {
t.Fatalf("Failed to fetch file attributes: %v", err)
}
if name, ok := attrs.Metadata["original_filename"]; !ok || name != "test" {
t.Fatalf("Expected original_filename to be %q, got %q", "test", name)
}
}
func TestFileSystemUpload(t *testing.T) {