Add retain_folder_name_extension option

This commit is contained in:
Ben Sarmiento
2023-11-07 17:20:54 +01:00
parent 3cc5044bc5
commit 0a76fdb14b
10 changed files with 41 additions and 74 deletions

View File

@@ -1,11 +1,7 @@
package davextra
import (
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"
)
// GetLinkFragment returns the longest alphanumeric string from a link
@@ -20,33 +16,3 @@ func GetLinkFragment(link string) string {
}
return longestMatch[:4]
}
// InsertLinkFragment inserts the link ID into a filename
// returns the new filename
func InsertLinkFragment(filename, dupeID string) string {
ext := filepath.Ext(filename)
name := strings.TrimSuffix(filename, ext)
return fmt.Sprintf("%s DMM%s%s", name, dupeID, ext)
}
// ExtractLinkFragment extracts the link ID from a filename
// returns the original filename and the link ID
func ExtractLinkFragment(filename string) (string, string) {
filenameV2, err := url.PathUnescape(filename)
if err != nil {
filenameV2 = filename
}
ext := filepath.Ext(filenameV2)
name := strings.TrimSuffix(filenameV2, ext)
r := regexp.MustCompile(`\sDMM(\w+)`)
matches := r.FindStringSubmatch(name)
if len(matches) < 2 {
// No ID found
return filenameV2, ""
}
// Remove ID from filename
originalName := strings.Replace(name, matches[0], "", 1)
return originalName + ext, matches[1]
}