mirror of
https://github.com/rclone/rclone.git
synced 2024-12-02 13:34:02 +08:00
85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
|
/*
|
|||
|
Translate file names for JottaCloud adapted from OneDrive
|
|||
|
|
|||
|
|
|||
|
The following characters are JottaClous reserved characters, and can't
|
|||
|
be used in JottaCloud folder and file names.
|
|||
|
|
|||
|
jottacloud = "/" / "\" / "*" / "<" / ">" / "?" / "!" / "&" / ":" / ";" / "|" / "#" / "%" / """ / "'" / "." / "~"
|
|||
|
|
|||
|
|
|||
|
*/
|
|||
|
|
|||
|
package jottacloud
|
|||
|
|
|||
|
import (
|
|||
|
"regexp"
|
|||
|
"strings"
|
|||
|
)
|
|||
|
|
|||
|
// charMap holds replacements for characters
|
|||
|
//
|
|||
|
// Onedrive has a restricted set of characters compared to other cloud
|
|||
|
// storage systems, so we to map these to the FULLWIDTH unicode
|
|||
|
// equivalents
|
|||
|
//
|
|||
|
// http://unicode-search.net/unicode-namesearch.pl?term=SOLIDUS
|
|||
|
var (
|
|||
|
charMap = map[rune]rune{
|
|||
|
'\\': '\', // FULLWIDTH REVERSE SOLIDUS
|
|||
|
'+': '+', // FULLWIDTH PLUS SIGN
|
|||
|
'*': '*', // FULLWIDTH ASTERISK
|
|||
|
'<': '<', // FULLWIDTH LESS-THAN SIGN
|
|||
|
'>': '>', // FULLWIDTH GREATER-THAN SIGN
|
|||
|
'?': '?', // FULLWIDTH QUESTION MARK
|
|||
|
'!': '!', // FULLWIDTH EXCLAMATION MARK
|
|||
|
'&': '&', // FULLWIDTH AMPERSAND
|
|||
|
':': ':', // FULLWIDTH COLON
|
|||
|
';': ';', // FULLWIDTH SEMICOLON
|
|||
|
'|': '|', // FULLWIDTH VERTICAL LINE
|
|||
|
'#': '#', // FULLWIDTH NUMBER SIGN
|
|||
|
'%': '%', // FULLWIDTH PERCENT SIGN
|
|||
|
'"': '"', // FULLWIDTH QUOTATION MARK - not on the list but seems to be reserved
|
|||
|
'\'': ''', // FULLWIDTH APOSTROPHE
|
|||
|
'~': '~', // FULLWIDTH TILDE
|
|||
|
' ': '␠', // SYMBOL FOR SPACE
|
|||
|
}
|
|||
|
invCharMap map[rune]rune
|
|||
|
fixStartingWithSpace = regexp.MustCompile(`(/|^) `)
|
|||
|
fixEndingWithSpace = regexp.MustCompile(` (/|$)`)
|
|||
|
)
|
|||
|
|
|||
|
func init() {
|
|||
|
// Create inverse charMap
|
|||
|
invCharMap = make(map[rune]rune, len(charMap))
|
|||
|
for k, v := range charMap {
|
|||
|
invCharMap[v] = k
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// replaceReservedChars takes a path and substitutes any reserved
|
|||
|
// characters in it
|
|||
|
func replaceReservedChars(in string) string {
|
|||
|
// Filenames can't start with space
|
|||
|
in = fixStartingWithSpace.ReplaceAllString(in, "$1"+string(charMap[' ']))
|
|||
|
// Filenames can't end with space
|
|||
|
in = fixEndingWithSpace.ReplaceAllString(in, string(charMap[' '])+"$1")
|
|||
|
return strings.Map(func(c rune) rune {
|
|||
|
if replacement, ok := charMap[c]; ok && c != ' ' {
|
|||
|
return replacement
|
|||
|
}
|
|||
|
return c
|
|||
|
}, in)
|
|||
|
}
|
|||
|
|
|||
|
// restoreReservedChars takes a path and undoes any substitutions
|
|||
|
// made by replaceReservedChars
|
|||
|
func restoreReservedChars(in string) string {
|
|||
|
return strings.Map(func(c rune) rune {
|
|||
|
if replacement, ok := invCharMap[c]; ok {
|
|||
|
return replacement
|
|||
|
}
|
|||
|
return c
|
|||
|
}, in)
|
|||
|
}
|