Add: support delete file

This commit is contained in:
2021-12-16 11:41:07 +08:00
parent 0edc7f7141
commit 435e3605f7
5 changed files with 188 additions and 54 deletions

View File

@@ -79,6 +79,7 @@ func NewAPI(config Config) (*API, error) {
apiMux.HandleFunc("/get_file_info", api.HandleGetFileInfo)
apiMux.HandleFunc("/get_file_stream_direct", api.HandleGetFileStreamDirect)
apiMux.HandleFunc("/prepare_file_stream_direct", api.HandlePrepareFileStreamDirect)
apiMux.HandleFunc("/delete_file", api.HandleDeleteFile)
// feedback
apiMux.HandleFunc("/feedback", api.HandleFeedback)
apiMux.HandleFunc("/get_feedbacks", api.HandleGetFeedbacks)

View File

@@ -0,0 +1,34 @@
package api
import (
"encoding/json"
"net/http"
)
type DeleteFileRequest struct {
ID int64 `json:"id"`
}
func (api *API) HandleDeleteFile(w http.ResponseWriter, r *http.Request) {
// check admin
err := api.CheckAdmin(w, r)
if err != nil {
api.HandleError(w, r, err)
return
}
req := &DeleteFileRequest{}
err = json.NewDecoder(r.Body).Decode(req)
if err != nil {
api.HandleError(w, r, err)
return
}
err = api.Db.DeleteFile(req.ID)
if err != nil {
api.HandleError(w, r, err)
return
}
api.HandleOK(w, r)
}