mirror of
https://github.com/keven1024/015.git
synced 2026-05-31 09:29:35 +00:00
30 lines
666 B
Go
30 lines
666 B
Go
package services
|
|
|
|
import (
|
|
"errors"
|
|
"worker/internal/utils"
|
|
)
|
|
|
|
func CompressImage(filePath string, mimeType string) (string, error) {
|
|
compressedPath := filePath + "_compressed"
|
|
switch mimeType {
|
|
case "image/png":
|
|
_, err := utils.RunCommand("pngquant", "--output", compressedPath, filePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
case "image/jpeg":
|
|
err := utils.CopyFile(filePath, compressedPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_, err = utils.RunCommand("jpegoptim", "-m", "80", "--strip-all", compressedPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
default:
|
|
return "", errors.New("不支持的文件类型")
|
|
}
|
|
return compressedPath, nil
|
|
}
|