2
* ALR - Any Linux Repository
3
* Copyright (C) 2024 Евгений Храмов
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.
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.
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/>.
29
"plemya-x.ru/alr/internal/config"
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")
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) {
45
itemPath := filepath.Join(BasePath(ctx), h)
47
fi, err := os.Stat(itemPath)
48
if err == nil || (fi != nil && !fi.IsDir()) {
49
err = os.RemoveAll(itemPath)
55
err = os.MkdirAll(itemPath, 0o755)
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
68
func Get(ctx context.Context, id string) (string, bool) {
73
itemPath := filepath.Join(BasePath(ctx), h)
75
_, err = os.Stat(itemPath)
83
// hashID hashes the input ID with SHA1
84
// and returns the hex string of the hashed
86
func hashID(id string) (string, error) {
88
_, err := io.WriteString(h, id)
92
return hex.EncodeToString(h.Sum(nil)), nil