mirror of
https://github.com/putyy/res-downloader.git
synced 2026-01-12 14:14:55 +08:00
perf: interception type optimization
This commit is contained in:
@@ -133,7 +133,7 @@ func (a *App) Startup(ctx context.Context) {
|
||||
if a.isInstall() {
|
||||
return
|
||||
}
|
||||
err := os.MkdirAll(a.UserDir, os.ModePerm)
|
||||
err := os.MkdirAll(a.UserDir, 0750)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -195,7 +195,7 @@ func (a *App) isInstall() bool {
|
||||
}
|
||||
|
||||
func (a *App) lock() error {
|
||||
err := os.WriteFile(a.LockFile, []byte("success"), 0777)
|
||||
err := os.WriteFile(a.LockFile, []byte("success"), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
107
core/config.go
107
core/config.go
@@ -5,28 +5,39 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type MimeInfo struct {
|
||||
Type string `json:"Type"`
|
||||
Suffix string `json:"Suffix"`
|
||||
}
|
||||
|
||||
// Config struct
|
||||
type Config struct {
|
||||
storage *Storage
|
||||
Theme string `json:"Theme"`
|
||||
Host string `json:"Host"`
|
||||
Port string `json:"Port"`
|
||||
Quality int `json:"Quality"`
|
||||
SaveDirectory string `json:"SaveDirectory"`
|
||||
FilenameLen int `json:"FilenameLen"`
|
||||
FilenameTime bool `json:"FilenameTime"`
|
||||
UpstreamProxy string `json:"UpstreamProxy"`
|
||||
OpenProxy bool `json:"OpenProxy"`
|
||||
DownloadProxy bool `json:"DownloadProxy"`
|
||||
AutoProxy bool `json:"AutoProxy"`
|
||||
WxAction bool `json:"WxAction"`
|
||||
TaskNumber int `json:"TaskNumber"`
|
||||
UserAgent string `json:"UserAgent"`
|
||||
UseHeaders string `json:"UseHeaders"`
|
||||
Theme string `json:"Theme"`
|
||||
Host string `json:"Host"`
|
||||
Port string `json:"Port"`
|
||||
Quality int `json:"Quality"`
|
||||
SaveDirectory string `json:"SaveDirectory"`
|
||||
FilenameLen int `json:"FilenameLen"`
|
||||
FilenameTime bool `json:"FilenameTime"`
|
||||
UpstreamProxy string `json:"UpstreamProxy"`
|
||||
OpenProxy bool `json:"OpenProxy"`
|
||||
DownloadProxy bool `json:"DownloadProxy"`
|
||||
AutoProxy bool `json:"AutoProxy"`
|
||||
WxAction bool `json:"WxAction"`
|
||||
TaskNumber int `json:"TaskNumber"`
|
||||
UserAgent string `json:"UserAgent"`
|
||||
UseHeaders string `json:"UseHeaders"`
|
||||
MimeMap map[string]MimeInfo `json:"MimeMap"`
|
||||
}
|
||||
|
||||
var (
|
||||
mimeMux sync.RWMutex
|
||||
)
|
||||
|
||||
func initConfig() *Config {
|
||||
if globalConfig == nil {
|
||||
def := `
|
||||
@@ -45,7 +56,66 @@ func initConfig() *Config {
|
||||
"WxAction": true,
|
||||
"TaskNumber": __TaskNumber__,
|
||||
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
|
||||
"UseHeaders": "User-Agent,Referer,Authorization,Cookie"
|
||||
"UseHeaders": "User-Agent,Referer,Authorization,Cookie",
|
||||
"MimeMap": {
|
||||
"image/png": { "Type": "image", "Suffix": ".png" },
|
||||
"image/webp": { "Type": "image", "Suffix": ".webp" },
|
||||
"image/jpeg": { "Type": "image", "Suffix": ".jpeg" },
|
||||
"image/jpg": { "Type": "image", "Suffix": ".jpg" },
|
||||
"image/gif": { "Type": "image", "Suffix": ".gif" },
|
||||
"image/avif": { "Type": "image", "Suffix": ".avif" },
|
||||
"image/bmp": { "Type": "image", "Suffix": ".bmp" },
|
||||
"image/tiff": { "Type": "image", "Suffix": ".tiff" },
|
||||
"image/heic": { "Type": "image", "Suffix": ".heic" },
|
||||
"image/x-icon": { "Type": "image", "Suffix": ".ico" },
|
||||
"image/svg+xml": { "Type": "image", "Suffix": ".svg" },
|
||||
"image/vnd.adobe.photoshop": { "Type": "image", "Suffix": ".psd" },
|
||||
"image/jp2": { "Type": "image", "Suffix": ".jp2" },
|
||||
"image/jpeg2000": { "Type": "image", "Suffix": ".jp2" },
|
||||
"image/apng": { "Type": "image", "Suffix": ".apng" },
|
||||
"audio/mpeg": { "Type": "audio", "Suffix": ".mp3" },
|
||||
"audio/mp3": { "Type": "audio", "Suffix": ".mp3" },
|
||||
"audio/wav": { "Type": "audio", "Suffix": ".wav" },
|
||||
"audio/aiff": { "Type": "audio", "Suffix": ".aiff" },
|
||||
"audio/x-aiff": { "Type": "audio", "Suffix": ".aiff" },
|
||||
"audio/aac": { "Type": "audio", "Suffix": ".aac" },
|
||||
"audio/ogg": { "Type": "audio", "Suffix": ".ogg" },
|
||||
"audio/flac": { "Type": "audio", "Suffix": ".flac" },
|
||||
"audio/midi": { "Type": "audio", "Suffix": ".mid" },
|
||||
"audio/x-midi": { "Type": "audio", "Suffix": ".mid" },
|
||||
"audio/x-ms-wma": { "Type": "audio", "Suffix": ".wma" },
|
||||
"audio/opus": { "Type": "audio", "Suffix": ".opus" },
|
||||
"audio/webm": { "Type": "audio", "Suffix": ".webm" },
|
||||
"audio/mp4": { "Type": "audio", "Suffix": ".m4a" },
|
||||
"audio/amr": { "Type": "audio", "Suffix": ".amr" },
|
||||
"video/mp4": { "Type": "video", "Suffix": ".mp4" },
|
||||
"video/webm": { "Type": "video", "Suffix": ".webm" },
|
||||
"video/ogg": { "Type": "video", "Suffix": ".ogv" },
|
||||
"video/x-msvideo": { "Type": "video", "Suffix": ".avi" },
|
||||
"video/mpeg": { "Type": "video", "Suffix": ".mpeg" },
|
||||
"video/quicktime": { "Type": "video", "Suffix": ".mov" },
|
||||
"video/x-ms-wmv": { "Type": "video", "Suffix": ".wmv" },
|
||||
"video/3gpp": { "Type": "video", "Suffix": ".3gp" },
|
||||
"video/x-matroska": { "Type": "video", "Suffix": ".mkv" },
|
||||
"audio/video": { "Type": "live", "Suffix": ".flv" },
|
||||
"video/x-flv": { "Type": "live", "Suffix": ".flv" },
|
||||
"application/dash+xml": { "Type": "live", "Suffix": ".mpd" },
|
||||
"application/vnd.apple.mpegurl": { "Type": "m3u8", "Suffix": ".m3u8" },
|
||||
"application/x-mpegurl": { "Type": "m3u8", "Suffix": ".m3u8" },
|
||||
"application/x-mpeg": { "Type": "m3u8", "Suffix": ".m3u8" },
|
||||
"application/pdf": { "Type": "pdf", "Suffix": ".pdf" },
|
||||
"application/vnd.ms-powerpoint": { "Type": "ppt", "Suffix": ".ppt" },
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation": { "Type": "ppt", "Suffix": ".pptx" },
|
||||
"application/vnd.ms-excel": { "Type": "xls", "Suffix": ".xls" },
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": { "Type": "xls", "Suffix": ".xlsx" },
|
||||
"text/csv": { "Type": "xls", "Suffix": ".csv" },
|
||||
"application/msword": { "Type": "doc", "Suffix": ".doc" },
|
||||
"application/rtf": { "Type": "doc", "Suffix": ".rtf" },
|
||||
"text/rtf": { "Type": "doc", "Suffix": ".rtf" },
|
||||
"application/vnd.oasis.opendocument.text": { "Type": "doc", "Suffix": ".odt" },
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": { "Type": "doc", "Suffix": ".docx" },
|
||||
"font/woff": { "Type": "font", "Suffix": ".woff" }
|
||||
}
|
||||
}
|
||||
`
|
||||
def = strings.ReplaceAll(def, "__TaskNumber__", strconv.Itoa(runtime.NumCPU()*2))
|
||||
@@ -97,6 +167,11 @@ func (c *Config) setConfig(config Config) {
|
||||
if oldProxy != c.UpstreamProxy {
|
||||
proxyOnce.setTransport()
|
||||
}
|
||||
|
||||
mimeMux.Lock()
|
||||
c.MimeMap = config.MimeMap
|
||||
mimeMux.Unlock()
|
||||
|
||||
jsonData, err := json.Marshal(c)
|
||||
if err == nil {
|
||||
_ = globalConfig.storage.Store(jsonData)
|
||||
|
||||
@@ -380,7 +380,6 @@ func (h *HttpServer) batchImport(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
fileName := filepath.Join(globalConfig.SaveDirectory, "res-downloader-"+GetCurrentDateTimeFormatted()+".txt")
|
||||
// 0644 是文件权限:-rw-r--r--
|
||||
err := os.WriteFile(fileName, []byte(data.Content), 0644)
|
||||
if err != nil {
|
||||
h.error(w, err.Error())
|
||||
|
||||
@@ -45,7 +45,7 @@ func NewLogger(logFile bool, logPath string) *Logger {
|
||||
logfile *os.File
|
||||
err error
|
||||
)
|
||||
logfile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
logfile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ type WxFileDecodeResult struct {
|
||||
}
|
||||
|
||||
type Resource struct {
|
||||
mediaMark sync.Map
|
||||
resType map[string]bool
|
||||
resTypeMu sync.RWMutex
|
||||
mediaMark sync.Map
|
||||
resType map[string]bool
|
||||
resTypeMux sync.RWMutex
|
||||
}
|
||||
|
||||
func initResource() *Resource {
|
||||
@@ -62,15 +62,15 @@ func (r *Resource) markMedia(key string) {
|
||||
}
|
||||
|
||||
func (r *Resource) getResType(key string) (bool, bool) {
|
||||
r.resTypeMu.RLock()
|
||||
defer r.resTypeMu.RUnlock()
|
||||
r.resTypeMux.RLock()
|
||||
defer r.resTypeMux.RUnlock()
|
||||
value, ok := r.resType[key]
|
||||
return value, ok
|
||||
}
|
||||
|
||||
func (r *Resource) setResType(n []string) {
|
||||
r.resTypeMu.Lock()
|
||||
defer r.resTypeMu.Unlock()
|
||||
r.resTypeMux.Lock()
|
||||
defer r.resTypeMux.Unlock()
|
||||
r.resType = map[string]bool{
|
||||
"all": false,
|
||||
"image": false,
|
||||
|
||||
@@ -19,7 +19,7 @@ func NewStorage(filename string, def []byte) *Storage {
|
||||
|
||||
func (l *Storage) Load() ([]byte, error) {
|
||||
if !FileExist(l.fileName) {
|
||||
err := os.WriteFile(l.fileName, l.def, 0777)
|
||||
err := os.WriteFile(l.fileName, l.def, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (l *Storage) Load() ([]byte, error) {
|
||||
}
|
||||
|
||||
func (l *Storage) Store(data []byte) error {
|
||||
if err := os.WriteFile(l.fileName, data, 0777); err != nil {
|
||||
if err := os.WriteFile(l.fileName, data, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -25,7 +25,7 @@ func (s *SystemSetup) initCert() ([]byte, error) {
|
||||
return content, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
err = os.WriteFile(s.CertFile, appOnce.PublicCrt, 0777)
|
||||
err = os.WriteFile(s.CertFile, appOnce.PublicCrt, 0750)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -40,69 +40,17 @@ func FileExist(file string) bool {
|
||||
|
||||
func CreateDirIfNotExist(dir string) error {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
return os.MkdirAll(dir, 0777)
|
||||
return os.MkdirAll(dir, 0750)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TypeSuffix(mime string) (string, string) {
|
||||
switch strings.ToLower(mime) {
|
||||
case "image/png",
|
||||
"image/webp",
|
||||
"image/jpeg",
|
||||
"image/jpg",
|
||||
"image/gif",
|
||||
"image/avif",
|
||||
"image/bmp",
|
||||
"image/tiff",
|
||||
"image/heic",
|
||||
"image/x-icon",
|
||||
"image/svg+xml",
|
||||
"image/vnd.adobe.photoshop":
|
||||
return "image", ".png"
|
||||
case "audio/mpeg",
|
||||
"audio/wav",
|
||||
"audio/aiff",
|
||||
"audio/x-aiff",
|
||||
"audio/aac",
|
||||
"audio/ogg",
|
||||
"audio/flac",
|
||||
"audio/midi",
|
||||
"audio/x-midi",
|
||||
"audio/x-ms-wma",
|
||||
"audio/opus",
|
||||
"audio/webm",
|
||||
"audio/mp4",
|
||||
"audio/mp3":
|
||||
return "audio", ".mp3"
|
||||
case "video/mp4",
|
||||
"video/webm",
|
||||
"video/ogg",
|
||||
"video/x-msvideo",
|
||||
"video/mpeg",
|
||||
"video/quicktime",
|
||||
"video/x-ms-wmv",
|
||||
"video/3gpp",
|
||||
"video/x-matroska":
|
||||
return "video", ".mp4"
|
||||
case "audio/video",
|
||||
"video/x-flv":
|
||||
return "live", ".mp4"
|
||||
case "application/vnd.apple.mpegurl",
|
||||
"application/x-mpegurl":
|
||||
return "m3u8", ".m3u8"
|
||||
case "application/pdf":
|
||||
return "pdf", ".pdf"
|
||||
case "application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
||||
return "ppt", ".ppt"
|
||||
case "application/vnd.ms-excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
||||
return "xls", ".xls"
|
||||
case "application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
return "doc", ".doc"
|
||||
|
||||
mimeMux.RLock()
|
||||
defer mimeMux.RUnlock()
|
||||
mime = strings.ToLower(strings.Split(mime, ";")[0])
|
||||
if v, ok := globalConfig.MimeMap[mime]; ok {
|
||||
return v.Type, v.Suffix
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user