mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
135 lines
3.6 KiB
Go
135 lines
3.6 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2026 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/ai/log"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/txt"
|
|
)
|
|
|
|
// UploadTaskDocument
|
|
// @Title UploadTaskDocument
|
|
// @Tag Task API
|
|
// @Description upload document for a task and parse its text
|
|
// @Param id query string true "The id (owner/name) of the task"
|
|
// @Param file formData string true "The base64 encoded file data"
|
|
// @Param type formData string true "The file type/extension"
|
|
// @Param name formData string true "The file name"
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
// @router /upload-task-document [post]
|
|
func (c *ApiController) UploadTaskDocument() {
|
|
userName, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
taskId := c.Input().Get("id")
|
|
fileBase64 := c.GetString("file")
|
|
fileType := c.GetString("type")
|
|
fileName := c.GetString("name")
|
|
|
|
if taskId == "" || fileBase64 == "" || fileType == "" || fileName == "" {
|
|
c.ResponseError(c.T("application:Missing required parameters"))
|
|
return
|
|
}
|
|
|
|
// Get the task to verify ownership
|
|
task, err := object.GetTask(taskId)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if task == nil {
|
|
c.ResponseError(c.T("general:The task does not exist"))
|
|
return
|
|
}
|
|
|
|
// Check ownership for non-admins
|
|
if !c.IsAdmin() {
|
|
if task.Owner != userName {
|
|
c.ResponseError(c.T("auth:Unauthorized operation"))
|
|
return
|
|
}
|
|
}
|
|
|
|
// Validate file extension - only .docx and .pdf allowed
|
|
ext := strings.ToLower(filepath.Ext(fileName))
|
|
allowedExtensions := []string{".docx", ".pdf"}
|
|
isValid := slices.Contains(allowedExtensions, ext)
|
|
if !isValid {
|
|
c.ResponseError(c.T("resource:Only docx and pdf files are allowed"))
|
|
return
|
|
}
|
|
|
|
// Decode base64 file data
|
|
_, after, ok := strings.Cut(fileBase64, ",")
|
|
if !ok {
|
|
c.ResponseError(c.T("resource:Invalid file data format"))
|
|
return
|
|
}
|
|
|
|
fileBytes, err := base64.StdEncoding.DecodeString(after)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// Upload file to storage
|
|
filePath := fmt.Sprintf("cloud/task-documents/%s/%s", userName, fileName)
|
|
fileUrl, err := object.UploadFileToStorageSafe(userName, "file", "UploadTaskDocument", filePath, fileBytes)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// Parse document text
|
|
documentText, err := txt.GetParsedTextFromUrl(fileUrl, ext, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
// Log error but don't fail the upload
|
|
log.Error("Failed to parse text from %s: %v", fileUrl, err)
|
|
documentText = ""
|
|
}
|
|
|
|
// Update task with document URL and text
|
|
task.DocumentUrl = fileUrl
|
|
task.DocumentText = documentText
|
|
|
|
success, err := object.UpdateTask(taskId, task)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
if !success {
|
|
c.ResponseError(c.T("general:Failed to update"))
|
|
return
|
|
}
|
|
|
|
// Return both URL and parsed text
|
|
result := map[string]any{
|
|
"url": fileUrl,
|
|
"text": documentText,
|
|
}
|
|
c.ResponseOk(result)
|
|
}
|