Add: get avatar from file

This commit is contained in:
2022-12-01 23:05:34 +08:00
parent 9fdea6b169
commit 271c7e5c13
4 changed files with 71 additions and 2 deletions

View File

@@ -59,6 +59,7 @@ func NewAPI(config commonconfig.Config) (*API, error) {
apiMux.HandleFunc("/get_file_info", api.HandleGetFileInfo)
apiMux.HandleFunc("/get_file_ffprobe_info", api.HandleGetFileFfprobeInfo)
apiMux.HandleFunc("/get_file_stream_direct", api.HandleGetFileStreamDirect)
apiMux.HandleFunc("/get_file_avatar", api.HandelGetFileAvatar)
apiMux.HandleFunc("/prepare_file_stream_direct", api.HandlePrepareFileStreamDirect)
apiMux.HandleFunc("/delete_file", api.HandleDeleteFile)
apiMux.HandleFunc("/update_filename", api.HandleUpdateFilename)

46
pkg/api/handle_avatar.go Normal file
View File

@@ -0,0 +1,46 @@
package api
import (
"errors"
"log"
"net/http"
"os/exec"
"strconv"
)
func (api *API) HandelGetFileAvatar(w http.ResponseWriter, r *http.Request) {
var err error
q := r.URL.Query()
ids := q["id"]
if len(ids) == 0 {
err = errors.New(`parameter "id" can't be empty`)
api.HandleError(w, r, err)
return
}
id, err := strconv.Atoi(ids[0])
if err != nil {
api.HandleError(w, r, err)
return
}
file, err := api.Db.GetFile(int64(id))
if err != nil {
api.HandleError(w, r, err)
return
}
path, err := file.Path()
if err != nil {
api.HandleError(w, r, err)
return
}
log.Println("[api] Get avatar of file", path)
cmd := exec.Command("ffmpeg", "-i", path, "-c:v", "libwebp_anim", "-update", "1", "-f", "image2pipe", "-")
cmd.Stdout = w
w.Header().Set("Content-Type", "image/webp")
err = cmd.Run()
if err != nil {
w.Header().Set("Content-Type", "application/json")
api.HandleError(w, r, err)
return
}
}