@@ -3,6 +3,7 @@ package controller
33import (
44 "errors"
55 "fmt"
6+ "io"
67 "log"
78 "mime/multipart"
89 "net/http"
@@ -23,10 +24,16 @@ type TaskController struct {
2324 TaskService services.TaskService
2425 AppFileService services.AppFileService
2526 VisionService services.VisionService
27+ StorageService services.StorageService
2628}
2729
28- func NewTaskController (taskService services.TaskService , appFileService services.AppFileService , visionService services.VisionService ) * TaskController {
29- return & TaskController {TaskService : taskService , AppFileService : appFileService , VisionService : visionService }
30+ func NewTaskController (taskService services.TaskService , appFileService services.AppFileService , visionService services.VisionService , storageService services.StorageService ) * TaskController {
31+ return & TaskController {
32+ TaskService : taskService ,
33+ AppFileService : appFileService ,
34+ VisionService : visionService ,
35+ StorageService : storageService ,
36+ }
3037}
3138
3239func (c * TaskController ) GetUnarchivedTasks (ctx * gin.Context ) {
@@ -136,7 +143,6 @@ func (c *TaskController) CreateTask(ctx *gin.Context) {
136143// @Failure 500 {object} map[string]string
137144// @Router /tasks/{taskID}/upload [post]
138145func (c * TaskController ) UploadFileToTask (ctx * gin.Context ) {
139-
140146 // Get the Task ID from the route
141147 taskIdParam := ctx .Param ("taskID" )
142148 taskId , err := strconv .Atoi (taskIdParam )
@@ -164,13 +170,6 @@ func (c *TaskController) UploadFileToTask(ctx *gin.Context) {
164170 return
165171 }
166172
167- // Define the upload folder
168- folderPath := fmt .Sprintf ("uploads/%d" , taskId )
169- if err := os .MkdirAll (folderPath , os .ModePerm ); err != nil {
170- ctx .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to create upload directory" })
171- return
172- }
173-
174173 var uploadedImages []model.AppFile
175174 var wg sync.WaitGroup
176175 var mu sync.Mutex
@@ -192,21 +191,18 @@ func (c *TaskController) UploadFileToTask(ctx *gin.Context) {
192191 return
193192 }
194193
195- // Generate a unique filename
196- filename := fmt .Sprintf ("%d-%d%s" , taskId , index , fileExt )
197- savePath := filepath .Join (folderPath , filename )
198-
199- // Save the file
200- if err := ctx .SaveUploadedFile (file , savePath ); err != nil {
194+ // Upload to object storage
195+ url , err := c .StorageService .UploadFile (file , uint (taskId ), "upload" )
196+ if err != nil {
201197 ctx .JSON (http .StatusInternalServerError , gin.H {"error" : fmt .Sprintf ("Failed to save file %s" , file .Filename )})
202198 hasError = true
203199 return
204200 }
205201
206202 // Save metadata to DB
207203 image := model.AppFile {
208- Filename : filename ,
209- Url : fmt . Sprintf ( "/uploads/%d/%s" , taskId , filename ) ,
204+ Filename : file . Filename ,
205+ Url : url ,
210206 TaskId : uint (taskId ),
211207 FileType : "upload" ,
212208 }
@@ -232,37 +228,42 @@ func (c *TaskController) UploadFileToTask(ctx *gin.Context) {
232228 tx .Commit ()
233229
234230 go func () {
235- // Generate caption
236- result , err := c .VisionService .AnalyseImage (fmt .Sprintf ("./uploads/%d/%s" , taskId , uploadedImages [0 ].Filename ), "" )
231+ // Get the first image URL from object storage
232+ file , err := c .StorageService .GetFile (fmt .Sprintf ("uploads/%d/%s" , taskId , uploadedImages [0 ].Filename ))
233+ if err != nil {
234+ log .Printf ("Unable to get image for analysis: %v" , err )
235+ return
236+ }
237+ defer file .Close ()
237238
239+ // Create a temporary file
240+ tempFile , err := os .CreateTemp ("" , "analysis-*.jpg" )
238241 if err != nil {
239- log .Printf ("Unable to analyze the image : %v" , err )
242+ log .Printf ("Unable to create temp file : %v" , err )
240243 return
241244 }
245+ defer os .Remove (tempFile .Name ())
246+ defer tempFile .Close ()
242247
243- if err := c .TaskService .UpdateMeta (& task , "ai-description" , result ); err != nil {
244- log .Printf ("Failed to update task metadata: %v" , err )
248+ // Copy the file content
249+ if _ , err := io .Copy (tempFile , file ); err != nil {
250+ log .Printf ("Unable to copy file content: %v" , err )
251+ return
245252 }
246- }()
247253
248- go func () {
249254 // Generate caption
250- result , err := c .VisionService .AnalyseImage (fmt .Sprintf ("./uploads/%d/%s" , taskId , uploadedImages [0 ].Filename ), "categorize the model in this image, use one word only" )
251-
255+ result , err := c .VisionService .AnalyseImage (tempFile .Name (), "" )
252256 if err != nil {
253257 log .Printf ("Unable to analyze the image: %v" , err )
254258 return
255259 }
256260
257- if err := c .TaskService .UpdateMeta (& task , "ai-title " , result ); err != nil {
261+ if err := c .TaskService .UpdateMeta (& task , "ai-description " , result ); err != nil {
258262 log .Printf ("Failed to update task metadata: %v" , err )
259263 }
260264 }()
261265
262- ctx .JSON (http .StatusOK , gin.H {
263- "message" : "Files uploaded successfully" ,
264- "images" : uploadedImages ,
265- })
266+ ctx .JSON (http .StatusOK , gin.H {"message" : "Files uploaded successfully" , "images" : uploadedImages })
266267}
267268
268269// StartProcess handles the process of starting the photogrammetry process
@@ -375,7 +376,7 @@ func (c *TaskController) SendMessage(ctx *gin.Context) {
375376 return
376377 }
377378
378- imagePath := fmt .Sprintf ("./ uploads/%d/%s" , taskId , task .Images [0 ].Filename )
379+ imagePath := fmt .Sprintf ("uploads/%d/%s" , taskId , task .Images [0 ].Filename )
379380
380381 if _ , err := os .Stat (imagePath ); os .IsNotExist (err ) {
381382 log .Printf ("Image file does not exist: %v\n " , err )
0 commit comments