From 08a5650b300c46d665f4483e2a5ec8c1679a2f38 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 3 Nov 2022 00:43:04 +0800 Subject: [PATCH] add api /v1/get_file_ffprobe_info --- pkg/api/api.go | 1 + pkg/api/handle_get_file_info.go | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/api/api.go b/pkg/api/api.go index fe3a293..5f63a6e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -57,6 +57,7 @@ func NewAPI(config commonconfig.Config) (*API, error) { apiMux.HandleFunc("/get_file_stream", api.HandleGetFileStream) apiMux.HandleFunc("/get_ffmpeg_config_list", api.HandleGetFfmpegConfigs) 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("/prepare_file_stream_direct", api.HandlePrepareFileStreamDirect) apiMux.HandleFunc("/delete_file", api.HandleDeleteFile) diff --git a/pkg/api/handle_get_file_info.go b/pkg/api/handle_get_file_info.go index 3aafbe1..5137247 100644 --- a/pkg/api/handle_get_file_info.go +++ b/pkg/api/handle_get_file_info.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "os/exec" "strconv" ) @@ -46,6 +47,45 @@ func (api *API) HandleGetFileInfo(w http.ResponseWriter, r *http.Request) { } } +func (api *API) HandleGetFileFfprobeInfo(w http.ResponseWriter, r *http.Request) { + getFileRequest := &GetFileRequest { + ID: -1, + } + + err := json.NewDecoder(r.Body).Decode(getFileRequest) + if err != nil { + api.HandleError(w, r, err) + return + } + + // check empty + if getFileRequest.ID < 0 { + api.HandleErrorString(w, r, `"id" can't be none or negative`) + return + } + + log.Println("[api] Get file Ffprobe info", getFileRequest.ID) + + file, err := api.Db.GetFile(getFileRequest.ID) + if err != nil { + api.HandleError(w, r, err) + return + } + + path, err := file.Path() + if err != nil { + api.HandleError(w, r, err) + return + } + cmd := exec.Command("ffprobe", "-i", path, "-hide_banner") + cmd.Stderr = w + err = cmd.Run() + if err != nil { + api.HandleError(w, r, err) + return + } +} + // /get_file // get raw file with io.Copy method func (api *API) HandleGetFile(w http.ResponseWriter, r *http.Request) {