mirror of
https://github.com/rclone/rclone.git
synced 2025-04-16 16:18:52 +08:00
lsjson: revert changes
This commit is contained in:
parent
177646e1d1
commit
d121aec1a0
@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/textproto"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
@ -155,29 +153,6 @@ func newListJSON(ctx context.Context, fsrc fs.Fs, remote string, opt *ListJSONOp
|
||||
return lj, nil
|
||||
}
|
||||
|
||||
// parseFilenameFromContentDisposition extracts the filename from a Content-Disposition header
|
||||
func parseFilenameFromContentDisposition(header string) (string, error) {
|
||||
// Normalize the header to canonical MIME format
|
||||
mediaType, params, err := mime.ParseMediaType(header)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse Content-Disposition header: %v", err)
|
||||
}
|
||||
|
||||
// Check if the header is an attachment
|
||||
if strings.ToLower(mediaType) != "attachment" {
|
||||
return "", fmt.Errorf("not an attachment: %s", mediaType)
|
||||
}
|
||||
|
||||
// Extract the filename from the parameters
|
||||
filename, ok := params["filename"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("filename not found in Content-Disposition header")
|
||||
}
|
||||
|
||||
// Decode filename if it contains special encoding
|
||||
return textproto.TrimString(filename), nil
|
||||
}
|
||||
|
||||
// Convert a single entry to JSON
|
||||
//
|
||||
// It may return nil if there is no entry to return
|
||||
@ -195,32 +170,9 @@ func (lj *listJSON) entry(ctx context.Context, entry fs.DirEntry) (*ListJSONItem
|
||||
fs.Errorf(nil, "Unknown type %T in listing", entry)
|
||||
}
|
||||
|
||||
// Get default name
|
||||
name := path.Base(entry.Remote())
|
||||
|
||||
// Extract the name from the metadata if requested
|
||||
if lj.opt.Metadata {
|
||||
// Get metadata
|
||||
metadata, err := fs.GetMetadata(ctx, entry)
|
||||
if err != nil {
|
||||
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 err != nil {
|
||||
fs.Errorf(entry, "Failed to parse filename from content-disposition: %v", err)
|
||||
}
|
||||
}
|
||||
if parsedName != "" {
|
||||
name = parsedName
|
||||
}
|
||||
}
|
||||
|
||||
item := &ListJSONItem{
|
||||
Path: entry.Remote(),
|
||||
Name: name,
|
||||
Name: path.Base(entry.Remote()),
|
||||
Size: entry.Size(),
|
||||
}
|
||||
if entry.Remote() == "" {
|
||||
|
@ -2,7 +2,6 @@ package operations_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
@ -10,7 +9,6 @@ import (
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/operations"
|
||||
"github.com/rclone/rclone/fstest"
|
||||
"github.com/rclone/rclone/fstest/fstests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@ -406,18 +404,3 @@ func TestStatJSON(t *testing.T) {
|
||||
assert.True(t, err != nil || f.Features().BucketBased, "Need an error for non bucket based backends")
|
||||
})
|
||||
}
|
||||
|
||||
func TestStatJsonMetadataContentDisposition(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Custom metadata is not supported on Windows")
|
||||
}
|
||||
ctx := context.Background()
|
||||
r := fstest.NewRun(t)
|
||||
file1 := fstest.Item{Path: "file1", ModTime: t1, Size: 5}
|
||||
metadata := fs.Metadata{"content-disposition": "Attachment; filename=file1.txt"}
|
||||
fstests.PutTestContentsMetadata(ctx, t, r.Fremote, &file1, false, "file1", true, "", metadata)
|
||||
got, err := operations.StatJSON(ctx, r.Fremote, "file1", &operations.ListJSONOpt{Metadata: true})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, got)
|
||||
assert.Equal(t, "file1.txt", got.Name)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user