mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 05:32:02 +08:00
Remove metadata when original not exist, update support for heic (#266)
* Remove metadata when original not exist, update support for heic * Warnf to Warnln
This commit is contained in:
parent
ab4373263d
commit
d9da7f3cd3
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user