From 69bfc759a26092acce91e27c14d08fd6ac2bb6b4 Mon Sep 17 00:00:00 2001 From: elvis Date: Mon, 31 Jul 2023 10:08:55 +0800 Subject: [PATCH] add support for .jpe (#24), fix long file path issue (#23) --- downloader/downloader.go | 6 +++++- kemono/fetch.go | 12 ++++++++---- main/main.go | 6 ++++++ main/path.go | 2 +- utils/helper.go | 8 ++++---- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/downloader/downloader.go b/downloader/downloader.go index ba8a7d8..019e44e 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -199,7 +199,11 @@ func SetLog(log Log) DownloadOption { func defaultSavePath(creator kemono.Creator, post kemono.Post, i int, attachment kemono.File) string { var name string - if filepath.Ext(attachment.Path) == ".zip" { + ext := filepath.Ext(attachment.Name) + if ext == "" { + ext = filepath.Ext(attachment.Path) + } + if ext == ".zip" { name = attachment.Name } else { name = filepath.Base(attachment.Path) diff --git a/kemono/fetch.go b/kemono/fetch.go index 2c9704e..e0b1466 100644 --- a/kemono/fetch.go +++ b/kemono/fetch.go @@ -155,7 +155,11 @@ func AddIndexToAttachments(attachments []File) []FileWithIndex { images := 0 others := 0 for _, a := range attachments { - if isImage(a.Path) { + ext := filepath.Ext(a.Name) + if ext == "" { + ext = filepath.Ext(a.Path) + } + if isImage(ext) { files = append(files, a.Index(images)) images++ } else { @@ -166,9 +170,9 @@ func AddIndexToAttachments(attachments []File) []FileWithIndex { return files } -func isImage(filename string) bool { - switch filepath.Ext(filename) { - case ".jpg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".svg", ".ico", ".jpeg", ".jfif": +func isImage(ext string) bool { + switch ext { + case ".jpg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".svg", ".ico", ".jpeg", ".jfif", ".jpe": return true default: return false diff --git a/main/main.go b/main/main.go index 2b7f420..1ed25c4 100644 --- a/main/main.go +++ b/main/main.go @@ -286,6 +286,9 @@ func main() { } downloaderOptions = append(downloaderOptions, downloader.SavePath(func(creator kemono.Creator, post kemono.Post, i int, attachment kemono.File) string { ext := filepath.Ext(attachment.Name) + if ext == "" { + ext = filepath.Ext(attachment.Path) + } filehash := filepath.Base(attachment.Path)[0 : len(filepath.Base(attachment.Path))-len(filepath.Ext(attachment.Path))] filename := attachment.Name[0 : len(attachment.Name)-len(ext)] pathConfig := &PathConfig{ @@ -309,6 +312,9 @@ func main() { downloaderOptions = append(downloaderOptions, downloader.SavePath(func(creator kemono.Creator, post kemono.Post, i int, attachment kemono.File) string { ext := filepath.Ext(attachment.Name) + if ext == "" { + ext = filepath.Ext(attachment.Path) + } filehash := filepath.Base(attachment.Path)[0 : len(filepath.Base(attachment.Path))-len(filepath.Ext(attachment.Path))] filename := attachment.Name[0 : len(attachment.Name)-len(ext)] pathConfig := &PathConfig{ diff --git a/main/path.go b/main/path.go index 22ec7ec..565dc29 100644 --- a/main/path.go +++ b/main/path.go @@ -119,7 +119,7 @@ func (t *TmplCache) Execute(typ string, config *PathConfig) string { func getTyp(ext string) string { switch ext { - case ".jpg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".svg", ".ico", ".jpeg", ".jfif": + case ".jpg", ".png", ".gif", ".webp", ".bmp", ".tiff", ".svg", ".ico", ".jpeg", ".jfif", ".jpe": return "image" case ".mp4", ".webm", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".f4v", ".m4v", ".rmvb", ".rm", ".3gp", ".dat", ".ts", ".mts", ".vob": return "video" diff --git a/utils/helper.go b/utils/helper.go index 1ac9639..8ed9fdc 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -51,8 +51,8 @@ func ValidDirectoryName(name string) string { return r } s := strings.TrimSpace(strings.Map(validate, name)) - if len(s) > 255 { - s = s[:255] + if len(s) > 200 { + s = s[:200] } if s[len(s)-1] == '.' { s = s[:len(s)-1] @@ -68,8 +68,8 @@ func ValidDirectoryName(name string) string { return r } s := strings.TrimSpace(strings.Map(validate, name)) - if len(s) > 255 { - s = s[:255] + if len(s) > 200 { + s = s[:200] } if s[0] == '.' { s = s[1:]