summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorHeinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>2023-01-17 02:41:51 +0100
committerHeinrich 'Henrik' Langos <gumbo2000@noreply@mills.io>2023-02-12 12:23:49 +0100
commitc4d471c9a2bb2fccedbd6243449201f6bcea72a7 (patch)
tree305bf2210f25b73511dd31023e87987b8173ec46 /app
parent0e522db2d4f9cfac166105be1ec6d8b61d86572d (diff)
refactor: Extract copyFileFromFormToUploadDir from uploadHandler
Once we have some file in the upload_path directory, we only need to determine where it should go. The further processing can be passed on to a job queue and handled in the background.
Diffstat (limited to 'app')
-rw-r--r--app/app.go57
1 files changed, 39 insertions, 18 deletions
diff --git a/app/app.go b/app/app.go
index 4ca75f1..a6457a2 100644
--- a/app/app.go
+++ b/app/app.go
@@ -270,27 +270,12 @@ func (a *App) uploadHandler(respWriter http.ResponseWriter, request *http.Reques
// keeping the file extension from the upload file probably makes it easier for ffmpeg to
// read the file for transcoding later
- uploadedFile, err := ioutil.TempFile(
- a.Config.Server.UploadPath,
- fmt.Sprintf("tube-upload-*%s", filepath.Ext(videoFilenameFromUpload)),
- )
+ uploadedFile, err := copyFileFromFormToUploadDir(a, videoContentFromUpload, videoFilenameFromUpload, respWriter)
if err != nil {
- err := fmt.Errorf("error creating temporary file for uploading: %w", err)
- log.Error(err)
- http.Error(respWriter, err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(uploadedFile.Name())
- _, err = io.Copy(uploadedFile, videoContentFromUpload)
- if err != nil {
- err := fmt.Errorf("error writing file: %w", err)
- log.Error(err)
- http.Error(respWriter, err.Error(), http.StatusInternalServerError)
- return
- }
-
-
// create temporary file for transcoded video file
temporaryTranscodedFile, err := ioutil.TempFile(
a.Config.Server.UploadPath,
@@ -451,7 +436,38 @@ func (a *App) uploadHandler(respWriter http.ResponseWriter, request *http.Reques
}
}
-func getUploadedVideoFile(a *App, request *http.Request, respWriter http.ResponseWriter) (fileReader io.ReadCloser, fileName string, err error) {
+// Copies the data from the upload form to a temporary file in the
+// upload_dir of our server. Returns a *os.File handle on that file
+// or an error in err
+func copyFileFromFormToUploadDir(
+ a *App, videoContentFromUpload io.ReadCloser, videoFilenameFromUpload string,
+ respWriter http.ResponseWriter) (
+ uploadedFile *os.File, err error) {
+
+ uploadedFile, err = ioutil.TempFile(
+ a.Config.Server.UploadPath,
+ fmt.Sprintf("tube-upload-*%s", filepath.Ext(videoFilenameFromUpload)),
+ )
+ if err != nil {
+ err := fmt.Errorf("error creating temporary file for uploading: %w", err)
+ log.Error(err)
+ http.Error(respWriter, err.Error(), http.StatusInternalServerError)
+ return nil, err
+ }
+
+ _, err = io.Copy(uploadedFile, videoContentFromUpload)
+ if err != nil {
+ err := fmt.Errorf("error writing file: %w", err)
+ log.Error(err)
+ http.Error(respWriter, err.Error(), http.StatusInternalServerError)
+ return nil, err
+ }
+ return uploadedFile, nil
+}
+
+func getUploadedVideoFile(
+ a *App, request *http.Request, respWriter http.ResponseWriter) (
+ fileReader io.ReadCloser, fileName string, err error) {
fileReader, fileHeaderFromUpload, err := request.FormFile("video_file")
if err != nil {
err := fmt.Errorf("error processing form: %w", err)
@@ -463,7 +479,12 @@ func getUploadedVideoFile(a *App, request *http.Request, respWriter http.Respons
return
}
-func getSelectedTargetLibraryDir(a *App, request *http.Request, respWriter http.ResponseWriter) (targetLibraryDirectory string, err error) {
+// infer the location where the uploaded and transcode video shall be stored
+// for now this is a directory, but in the future we will return a library
+// location that could point to a different type, like an s3 bucket.
+func getSelectedTargetLibraryDir(
+ a *App, request *http.Request, respWriter http.ResponseWriter) (
+ targetLibraryDirectory string, err error) {
if _, exists := a.Library.Paths[request.FormValue("target_library_path")]; !exists {
err = fmt.Errorf("uploading to invalid library path: %s", request.FormValue("target_library_path"))
log.Error(err)