mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-01-26 06:55:51 +02:00
store the original uploaded file name as metadata
This commit is contained in:
parent
b8cd686b32
commit
599c542c5a
@ -133,8 +133,17 @@ func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
|
|||||||
// rewind
|
// rewind
|
||||||
f.Seek(0, io.SeekStart)
|
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{
|
opts := &blob.WriterOptions{
|
||||||
ContentType: mt.String(),
|
ContentType: mt.String(),
|
||||||
|
Metadata: map[string]string{
|
||||||
|
"original_filename": fh.Filename,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := s.bucket.NewWriter(s.ctx, fileKey, opts)
|
w, err := s.bucket.NewWriter(s.ctx, fileKey, opts)
|
||||||
|
@ -162,14 +162,24 @@ func TestFileSystemUploadMultipart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer fs.Close()
|
defer fs.Close()
|
||||||
|
|
||||||
uploadErr := fs.UploadMultipart(fh, "newdir/newkey.txt")
|
fileKey := "newdir/newkey.txt"
|
||||||
|
|
||||||
|
uploadErr := fs.UploadMultipart(fh, fileKey)
|
||||||
if uploadErr != nil {
|
if uploadErr != nil {
|
||||||
t.Fatal(uploadErr)
|
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")
|
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) {
|
func TestFileSystemUpload(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user