| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | package api
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"io"
 | 
					
						
							|  |  |  | 	"openmediacenter/apiGo/api/api"
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 	"openmediacenter/apiGo/database"
 | 
					
						
							|  |  |  | 	"openmediacenter/apiGo/videoparser"
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2021-09-24 22:12:42 +02:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func addUploadHandler() {
 | 
					
						
							|  |  |  | 	api.AddHandler("fileupload", api.VideoNode, api.PermUser, func(ctx api.Context) {
 | 
					
						
							|  |  |  | 		fmt.Println("we are in file upload handler")
 | 
					
						
							| 
									
										
										
										
											2021-09-21 23:39:21 +02:00
										 |  |  | 		fmt.Printf("permission: %s\n", ctx.PermID().String())
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 		// get path where to store videos to
 | 
					
						
							|  |  |  | 		mSettings, PathPrefix, _ := database.GetSettings()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 		req := ctx.GetRequest()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mr, err := req.MultipartReader()
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2021-09-24 22:12:42 +02:00
										 |  |  | 			ctx.Errorf("incorrect request!")
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2021-09-24 22:12:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		videoparser.InitDeps(&mSettings)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 		//length := req.ContentLength
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 		for {
 | 
					
						
							|  |  |  | 			part, err := mr.NextPart()
 | 
					
						
							|  |  |  | 			if err == io.EOF {
 | 
					
						
							|  |  |  | 				break
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-24 22:12:42 +02:00
										 |  |  | 			// todo allow more video formats than mp4
 | 
					
						
							|  |  |  | 			// only allow valid extensions
 | 
					
						
							|  |  |  | 			if filepath.Ext(part.FileName()) != ".mp4" {
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 			vidpath := PathPrefix + mSettings.VideoPath + part.FileName()
 | 
					
						
							|  |  |  | 			dst, err := os.OpenFile(vidpath, os.O_WRONLY|os.O_CREATE, 0644)
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 			fmt.Printf("Uploading file %s\n", part.FileName())
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 			// so now loop through every appended file and upload
 | 
					
						
							|  |  |  | 			buffer := make([]byte, 100000)
 | 
					
						
							|  |  |  | 			for {
 | 
					
						
							|  |  |  | 				cBytes, err := part.Read(buffer)
 | 
					
						
							|  |  |  | 				if cBytes > 0 {
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 					//read = read + int64(cBytes)
 | 
					
						
							|  |  |  | 					//p = float32(read) / float32(length) * 100
 | 
					
						
							|  |  |  | 					//fmt.Printf("progress: %v \n", p)
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 					dst.Write(buffer[0:cBytes])
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if err == io.EOF {
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 					fmt.Printf("Finished uploading file %s\n", part.FileName())
 | 
					
						
							| 
									
										
										
										
											2021-09-24 22:12:42 +02:00
										 |  |  | 					go videoparser.ProcessVideo(part.FileName())
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 					break
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2021-09-21 23:39:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			_ = dst.Close()
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 17:38:20 +02:00
										 |  |  | 		ctx.Text("finished all files")
 | 
					
						
							| 
									
										
										
										
											2021-09-21 17:45:24 +02:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | }
 |