lsjon: add autofilename opt and draft for test

This commit is contained in:
hiddenmarten 2025-01-22 16:03:59 +01:00
parent 3b33c2202d
commit 64043c2942
2 changed files with 36 additions and 15 deletions

View File

@ -84,6 +84,7 @@ type ListJSONOpt struct {
DirsOnly bool `json:"dirsOnly"`
FilesOnly bool `json:"filesOnly"`
Metadata bool `json:"metadata"`
AutoFilename bool `json:"autoFilename"`
HashTypes []string `json:"hashTypes"` // hash types to show if ShowHash is set, e.g. "MD5", "SHA-1"
}
@ -194,22 +195,22 @@ func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem
fs.Errorf(nil, "Unknown type %T in listing", entry)
}
// Read the metadata if required
meta, err := fs.GetMetadata(ctx, entry)
if err != nil {
fs.Errorf(entry, "Failed to read metadata: %v", err)
}
// Extract the name from the metadata if possible
var name string
if meta != nil && meta["content-disposition"] != "" {
name, err = parseFilenameFromContentDisposition(meta["content-disposition"])
// Extract the name from the metadata if requested
name := path.Base(entry.Remote())
if lj.opt.AutoFilename {
// Get metadata
metadata, err := fs.GetMetadata(ctx, entry)
if err != nil {
fs.Errorf(entry, "Failed to parse filename from Content-Disposition: %v", err)
name = path.Base(entry.Remote())
fs.Errorf(entry, "Failed to read metadata: %v", err)
}
// Parse the filename from the metadata
var parsedName string
if metadata != nil && metadata["content-disposition"] != "" {
parsedName, err = parseFilenameFromContentDisposition(metadata["content-disposition"])
}
if parsedName != "" {
name = parsedName
}
} else {
name = path.Base(entry.Remote())
}
item := &ListJSONItem{

View File

@ -193,7 +193,6 @@ func TestListJSON(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
var got []*operations.ListJSONItem
require.NoError(t, operations.ListJSON(ctx, r.Fremote, test.remote, &test.opt, func(item *operations.ListJSONItem) error {
got = append(got, item)
return nil
}))
sort.Slice(got, func(i, j int) bool {
@ -385,6 +384,7 @@ func TestStatJSON(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
// Metadata update for "AutoFilename" test case
got, err := operations.StatJSON(ctx, r.Fremote, test.remote, &test.opt)
require.NoError(t, err)
if test.want == nil {
@ -404,3 +404,23 @@ func TestStatJSON(t *testing.T) {
assert.True(t, err != nil || f.Features().BucketBased, "Need an error for non bucket based backends")
})
}
//func TestAutoFilename(t *testing.T) {
// ctx := context.Background()
// r := fstest.NewRun(t)
// file1 := r.WriteBoth(ctx, "file1", "file1", t1)
// r.CheckRemoteItems(t, file1)
//
// // Metadata update for "AutoFilename" test case
// if item.Metadata == nil {
// item.Metadata = fs.Metadata{}
// }
// item.Metadata["Content-Disposition"] = "attachment; filename=\"file_name_from_metadata\""
//
// got, err := operations.StatJSON(ctx, r.Fremote, "file1", &operations.ListJSONOpt{
// AutoFilename: true,
// })
// require.NoError(t, err)
// require.NotNil(t, got)
// assert.Equal(t, "file_name_from_metadata", got.Name)
//}