2022-08-28 11:43:25 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-28 11:43:25 +02:00
|
|
|
|
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2023-03-08 10:31:27 +01:00
|
|
|
"path"
|
2022-08-28 11:43:25 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
2023-03-08 10:31:27 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-08-28 11:43:25 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
2023-03-08 10:31:27 +01:00
|
|
|
// Locale reads the content of a specific locale from static/bindata or custom path.
|
|
|
|
func Locale(name string) ([]byte, error) {
|
2023-03-08 13:17:39 +01:00
|
|
|
return fileFromDir(path.Join("locale", util.CleanPath(name)))
|
2023-03-08 10:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Readme reads the content of a specific readme from static/bindata or custom path.
|
|
|
|
func Readme(name string) ([]byte, error) {
|
2023-03-08 13:17:39 +01:00
|
|
|
return fileFromDir(path.Join("readme", util.CleanPath(name)))
|
2023-03-08 10:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gitignore reads the content of a gitignore locale from static/bindata or custom path.
|
|
|
|
func Gitignore(name string) ([]byte, error) {
|
2023-03-08 13:17:39 +01:00
|
|
|
return fileFromDir(path.Join("gitignore", util.CleanPath(name)))
|
2023-03-08 10:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// License reads the content of a specific license from static/bindata or custom path.
|
|
|
|
func License(name string) ([]byte, error) {
|
2023-03-08 13:17:39 +01:00
|
|
|
return fileFromDir(path.Join("license", util.CleanPath(name)))
|
2023-03-08 10:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Labels reads the content of a specific labels from static/bindata or custom path.
|
|
|
|
func Labels(name string) ([]byte, error) {
|
2023-03-08 13:17:39 +01:00
|
|
|
return fileFromDir(path.Join("label", util.CleanPath(name)))
|
2023-03-08 10:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WalkLocales reads the content of a specific locale
|
|
|
|
func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error {
|
|
|
|
if IsDynamic() {
|
|
|
|
if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("failed to walk locales. Error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("failed to walk locales. Error: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-28 11:43:25 +02:00
|
|
|
func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, err error) error) error {
|
|
|
|
if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
// name is the path relative to the root
|
|
|
|
name := path[len(root):]
|
|
|
|
if len(name) > 0 && name[0] == '/' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return callback(path, name, d, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if util.CommonSkip(d.Name()) {
|
|
|
|
if d.IsDir() {
|
|
|
|
return fs.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return callback(path, name, d, err)
|
|
|
|
}); err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("unable to get files for assets in %s: %w", root, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-08 10:31:27 +01:00
|
|
|
|
|
|
|
func statDirIfExist(dir string) ([]string, error) {
|
|
|
|
isDir, err := util.IsDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to check if static directory %s is a directory. %w", dir, err)
|
|
|
|
}
|
|
|
|
if !isDir {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
files, err := util.StatDir(dir, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read directory %q. %w", dir, err)
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|