This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathname.go
64 lines (54 loc) · 1.46 KB
/
name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2017 Tom Thorogood. All rights reserved.
// Use of this source code is governed by a Modified
// BSD License that can be found in the LICENSE file.
package bindata
import (
"encoding/base32"
"encoding/base64"
"encoding/hex"
"path"
"strings"
)
var base32Enc = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
// Name applies name hashing if required. It returns the original
// name for NoHash and NameUnchanged and returns the mangledName
// otherwise.
func (asset *binAsset) Name() string {
if asset.Hash == nil || asset.opts.HashFormat == NameUnchanged {
return asset.File.Name()
} else if asset.mangledName != "" {
return asset.mangledName
}
var enc string
switch asset.opts.HashEncoding {
case HexHash:
enc = hex.EncodeToString(asset.Hash)
case Base32Hash:
enc = base32Enc.EncodeToString(asset.Hash)
case Base64Hash:
enc = base64.RawURLEncoding.EncodeToString(asset.Hash)
default:
panic("unreachable")
}
l := asset.opts.HashLength
if l == 0 {
l = 16
}
if l < uint(len(enc)) {
enc = enc[:l]
}
dir, file := path.Split(asset.File.Name())
ext := path.Ext(file)
switch asset.opts.HashFormat {
case DirHash:
asset.mangledName = path.Join(dir, enc, file)
case NameHashSuffix:
file = strings.TrimSuffix(file, ext)
asset.mangledName = path.Join(dir, file+"-"+enc+ext)
case HashWithExt:
asset.mangledName = path.Join(dir, enc+ext)
default:
panic("unreachable")
}
return asset.mangledName
}