ALR

Форк
1
/
dlcache.go 
93 строки · 2.3 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package dlcache
20

21
import (
22
	"context"
23
	"crypto/sha1"
24
	"encoding/hex"
25
	"io"
26
	"os"
27
	"path/filepath"
28

29
	"plemya-x.ru/alr/internal/config"
30
)
31

32
// BasePath returns the base path of the download cache
33
func BasePath(ctx context.Context) string {
34
	return filepath.Join(config.GetPaths(ctx).CacheDir, "dl")
35
}
36

37
// New creates a new directory with the given ID in the cache.
38
// If a directory with the same ID already exists,
39
// it will be deleted before creating a new one.
40
func New(ctx context.Context, id string) (string, error) {
41
	h, err := hashID(id)
42
	if err != nil {
43
		return "", err
44
	}
45
	itemPath := filepath.Join(BasePath(ctx), h)
46

47
	fi, err := os.Stat(itemPath)
48
	if err == nil || (fi != nil && !fi.IsDir()) {
49
		err = os.RemoveAll(itemPath)
50
		if err != nil {
51
			return "", err
52
		}
53
	}
54

55
	err = os.MkdirAll(itemPath, 0o755)
56
	if err != nil {
57
		return "", err
58
	}
59

60
	return itemPath, nil
61
}
62

63
// Get checks if an entry with the given ID
64
// already exists in the cache, and if so,
65
// returns the directory and true. If it
66
// does not exist, it returns an empty string
67
// and false.
68
func Get(ctx context.Context, id string) (string, bool) {
69
	h, err := hashID(id)
70
	if err != nil {
71
		return "", false
72
	}
73
	itemPath := filepath.Join(BasePath(ctx), h)
74

75
	_, err = os.Stat(itemPath)
76
	if err != nil {
77
		return "", false
78
	}
79

80
	return itemPath, true
81
}
82

83
// hashID hashes the input ID with SHA1
84
// and returns the hex string of the hashed
85
// ID.
86
func hashID(id string) (string, error) {
87
	h := sha1.New()
88
	_, err := io.WriteString(h, id)
89
	if err != nil {
90
		return "", err
91
	}
92
	return hex.EncodeToString(h.Sum(nil)), nil
93
}
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.