From 90289401fa55686f704a2fe712aa4442e4cd827e Mon Sep 17 00:00:00 2001 From: Phuong Pham Date: Sun, 16 Feb 2025 13:41:50 -0600 Subject: [PATCH] fix code --- backend/azureblob/azureblob.go | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/backend/azureblob/azureblob.go b/backend/azureblob/azureblob.go index fcefd19c5..9860891ed 100644 --- a/backend/azureblob/azureblob.go +++ b/backend/azureblob/azureblob.go @@ -19,6 +19,7 @@ import ( "net/url" "os" "path" + "regexp" "sort" "strconv" "strings" @@ -1099,6 +1100,37 @@ func (o *Object) updateMetadataWithModTime(modTime time.Time) { o.meta[modTimeKey] = modTime.Format(timeFormatOut) } +// update meta data +func (o *Object) updateMetadata(ctx context.Context, src fs.ObjectInfo, options []fs.OpenOption) (ui uploadInfo, err error) { + metadataMu.Lock() + defer metadataMu.Unlock() + + // Make sure o.meta is not nil + if o.meta == nil { + o.meta = make(map[string]string, 1) + } + + meta, err := fs.GetMetadataOptions(ctx, o.fs, src, options) + if err != nil { + return ui, fmt.Errorf("failed to read metadata from source object: %w", err) + } + + for k, v := range meta { + // Azure does not allow special characters in key, so we replace all of them by empty string + var fixedKey = RemoveUnwantedChars(k, "a-zA-Z0-9") + o.meta[fixedKey] = v + } + + return ui, nil +} + +// Remove unwanted characters +func RemoveUnwantedChars(input string, allowedChars string) string { + pattern := fmt.Sprintf("[^%s]", allowedChars) + re := regexp.MustCompile(pattern) + return re.ReplaceAllString(input, "") +} + // Returns whether file is a directory marker or not func isDirectoryMarker(size int64, metadata map[string]*string, remote string) bool { // Directory markers are 0 length @@ -2851,6 +2883,12 @@ func (o *Object) prepareUpload(ctx context.Context, src fs.ObjectInfo, options [ return ui, err } + // Update other metadata + ui, err = o.updateMetadata(ctx, src, options) + if err != nil { + return ui, err + } + // Create the HTTP headers for the upload ui.httpHeaders = blob.HTTPHeaders{ BlobContentType: pString(fs.MimeType(ctx, src)),