From d9da7f3cd3c1a13cce368a81e15ecab162fd7afd Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Thu, 17 Aug 2023 18:30:53 +0800 Subject: [PATCH] Remove metadata when original not exist, update support for heic (#266) * Remove metadata when original not exist, update support for heic * Warnf to Warnln --- README.md | 5 ++++- config.json | 2 +- handler/router.go | 11 ++++++----- helper/metadata.go | 9 +++++++++ 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 87f9d55d..a31857e9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This is a Server based on Golang, which allows you to serve WebP images on the fly. -Currently supported image format: JPEG, PNG, BMP, GIF, SVG +Currently supported image format: JPEG, PNG, BMP, GIF, SVG, HEIC > e.g When you visit `https://your.website/pics/tsuki.jpg`,it will serve as `image/webp` format without changing the URL. @@ -35,6 +35,7 @@ services: volumes: - ./path/to/pics:/opt/pics - ./exhaust:/opt/exhaust + - ./metadata:/opt/metadata ports: - 127.0.0.1:3333:3333 ``` @@ -49,6 +50,7 @@ Then * `./path/to/pics` should be changed to `/var/www/img.webp.sh` * `./exhaust` is cache folder for output images, by default it will be in `exhaust` directory alongside with `docker-compose.yml` file, if you'd like to keep cached images in another folder, you can change `./exhaust` to `/some/other/path/to/exhaust` +* `./metadata` is cache folder for images' metadata, by default it will be in `metadata` directory alongside with `docker-compose.yml` file Start the container using: @@ -77,6 +79,7 @@ services: volumes: - ./path/to/pics:/opt/pics - ./path/to/exhaust:/opt/exhaust + - ./path/to/metadata:/opt/metadata - ./config.json:/etc/config.json ports: - 127.0.0.1:3333:3333 diff --git a/config.json b/config.json index a63cc6b3..831651ab 100644 --- a/config.json +++ b/config.json @@ -4,7 +4,7 @@ "QUALITY": "80", "IMG_PATH": "./pics", "EXHAUST_PATH": "./exhaust", - "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif","svg"], + "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif","svg","heic"], "IMG_MAP": {}, "ENABLE_AVIF": false, "ENABLE_EXTRA_PARAMS": false diff --git a/handler/router.go b/handler/router.go index 3ef1f2ce..7d86bd8f 100644 --- a/handler/router.go +++ b/handler/router.go @@ -78,11 +78,11 @@ func Convert(c *fiber.Ctx) error { targetHostUrl, _ := url.Parse(uriMapTarget) targetHostName = targetHostUrl.Host targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host - reqURI = strings.Replace(reqURI, uriMap, targetHostUrl.Path, 1) + reqURI = strings.Replace(reqURI, uriMap, targetHostUrl.Path, 1) reqURIwithQuery = strings.Replace(reqURIwithQuery, uriMap, targetHostUrl.Path, 1) proxyMode = true } else { - reqURI = strings.Replace(reqURI, uriMap, uriMapTarget, 1) + reqURI = strings.Replace(reqURI, uriMap, uriMapTarget, 1) reqURIwithQuery = strings.Replace(reqURIwithQuery, uriMap, uriMapTarget, 1) } break @@ -106,15 +106,15 @@ func Convert(c *fiber.Ctx) error { realRemoteAddr = targetHost + reqURIwithQuery log.Debugf("realRemoteAddr is %s", realRemoteAddr) } - + var rawImageAbs string var metadata = config.MetaFile{} if proxyMode { // this is proxyMode, we'll have to use this url to download and save it to local path, which also gives us rawImageAbs // https://test.webp.sh/mypic/123.jpg?someother=200&somebugs=200 - + metadata = fetchRemoteImg(realRemoteAddr, targetHostName) - rawImageAbs = path.Join(config.RemoteRaw, targetHostName, metadata.Id) + rawImageAbs = path.Join(config.RemoteRaw, targetHostName, metadata.Id) } else { // not proxyMode, we'll use local path metadata = helper.ReadMetadata(reqURIwithQuery, "", targetHostName) @@ -144,6 +144,7 @@ func Convert(c *fiber.Ctx) error { // Check the original image for existence, if !helper.ImageExists(rawImageAbs) { + helper.DeleteMetadata(reqURIwithQuery, targetHostName) msg := "image not found" _ = c.Send([]byte(msg)) log.Warn(msg) diff --git a/helper/metadata.go b/helper/metadata.go index 27711468..c197443f 100644 --- a/helper/metadata.go +++ b/helper/metadata.go @@ -68,3 +68,12 @@ func WriteMetadata(p, etag string, subdir string) config.MetaFile { _ = os.WriteFile(path.Join(config.Metadata, subdir, data.Id+".json"), buf, 0644) return data } + +func DeleteMetadata(p string, subdir string) { + var id, _, _ = getId(p) + metadataPath := path.Join(config.Metadata, subdir, id+".json") + err := os.Remove(metadataPath) + if err != nil { + log.Warnln("failed to delete metadata", err) + } +}