File: utils.go

package info (click to toggle)
golang-github-gophercloud-utils 0.0~git20200508.b0167b9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 784 kB
  • sloc: sh: 20; makefile: 7
file content (69 lines) | stat: -rw-r--r-- 1,164 bytes parent folder | download | duplicates (3)
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
65
66
67
68
69
package objects

import (
	"bufio"
	"crypto/md5"
	"fmt"
	"io"
	"os"
	"strings"
)

const (
	emptyETag  = "d41d8cd98f00b204e9800998ecf8427e"
	diskBuffer = 65536
)

var (
	knownDirMarkers = []string{
		"application/directory",
		"text/directory",
	}
)

func ContainerPartition(containerName string) (string, string) {
	var pseudoFolder string

	parts := strings.SplitN(containerName, "/", 2)
	if len(parts) == 2 {
		containerName = parts[0]
		pseudoFolder = strings.TrimSuffix(parts[1], "/")
	}

	return containerName, pseudoFolder
}

// https://github.com/holys/checksum/blob/master/md5/md5.go
func FileMD5Sum(filename string) (string, error) {
	if _, err := os.Stat(filename); err != nil {
		return "", err
	}

	file, err := os.Open(filename)
	if err != nil {
		return "", err
	}
	defer file.Close()

	hash := md5.New()

	for buf, reader := make([]byte, diskBuffer), bufio.NewReader(file); ; {
		n, err := reader.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}

			return "", err
		}

		hash.Write(buf[:n])
	}

	return fmt.Sprintf("%x", hash.Sum(nil)), nil
}

func GetContentType(ct string) string {
	v := strings.SplitN(ct, ";", 2)
	return v[0]
}