Skip to content

Commit

Permalink
Remove metadata when original not exist, update support for heic (#266)
Browse files Browse the repository at this point in the history
* Remove metadata when original not exist, update support for heic

* Warnf to Warnln
  • Loading branch information
n0vad3v committed Aug 17, 2023
1 parent ab43732 commit d9da7f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,6 +35,7 @@ services:
volumes:
- ./path/to/pics:/opt/pics
- ./exhaust:/opt/exhaust
- ./metadata:/opt/metadata
ports:
- 127.0.0.1:3333:3333
```
Expand All @@ -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:

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions helper/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d9da7f3

Please sign in to comment.