-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.go
46 lines (38 loc) · 911 Bytes
/
util.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
package zboxutil
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"github.com/h2non/filetype"
)
func GetFileContentType(out *os.File) (string, error) {
buffer := make([]byte, 261)
_, err := out.Read(buffer)
defer out.Seek(0, 0)
if err != nil {
return "", err
}
kind, _ := filetype.Match(buffer)
if kind == filetype.Unknown {
return "application/octet-stream", nil
}
return kind.MIME.Value, nil
}
func GetFullRemotePath(localPath, remotePath string) string {
if remotePath == "" || strings.HasSuffix(remotePath, "/") {
remotePath = strings.TrimRight(remotePath, "/")
_, fileName := filepath.Split(localPath)
remotePath = fmt.Sprintf("%s/%s", remotePath, fileName)
}
return remotePath
}
func NewConnectionId() string {
nBig, err := rand.Int(rand.Reader, big.NewInt(0xffffffff))
if err != nil {
panic(err)
}
return fmt.Sprintf("%d", nBig.Int64())
}