|
15 | 15 | package controllers |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "encoding/base64" |
19 | 18 | "encoding/json" |
20 | 19 | "fmt" |
21 | 20 | "mime" |
@@ -243,77 +242,3 @@ func (c *ApiController) UploadResourceFile() { |
243 | 242 |
|
244 | 243 | c.ResponseOk(fileUrl, fullFilePath) |
245 | 244 | } |
246 | | - |
247 | | -// UploadFile |
248 | | -// @Title UploadFile |
249 | | -// @Tag File API |
250 | | -// @Description upload a base64-encoded file and record it as a resource |
251 | | -// @Param file formData string true "The base64 encoded file data" |
252 | | -// @Param type formData string true "The file type/extension" |
253 | | -// @Param name formData string true "The file name" |
254 | | -// @Param category formData string false "Resource category: avatar (default), chat, document" |
255 | | -// @Param objectType formData string false "Associated object type" |
256 | | -// @Param objectId formData string false "Associated object id" |
257 | | -// @Success 200 {object} controllers.Response The Response object |
258 | | -// @router /upload-file [post] |
259 | | -func (c *ApiController) UploadFile() { |
260 | | - userName, ok := c.RequireSignedIn() |
261 | | - if !ok { |
262 | | - return |
263 | | - } |
264 | | - |
265 | | - fileBase64 := c.Input().Get("file") |
266 | | - fileType := c.Input().Get("type") |
267 | | - fileName := c.Input().Get("name") |
268 | | - category := c.Input().Get("category") |
269 | | - objectType := c.Input().Get("objectType") |
270 | | - objectId := c.Input().Get("objectId") |
271 | | - |
272 | | - if fileBase64 == "" || fileType == "" || fileName == "" { |
273 | | - c.ResponseError(c.T("application:Missing required parameters")) |
274 | | - return |
275 | | - } |
276 | | - |
277 | | - if category == "" { |
278 | | - category = "avatar" |
279 | | - } |
280 | | - |
281 | | - index := strings.Index(fileBase64, ",") |
282 | | - if index == -1 { |
283 | | - c.ResponseError(c.T("resource:Invalid file data format")) |
284 | | - return |
285 | | - } |
286 | | - |
287 | | - fileBytes, err := base64.StdEncoding.DecodeString(fileBase64[index+1:]) |
288 | | - if err != nil { |
289 | | - c.ResponseError(err.Error()) |
290 | | - return |
291 | | - } |
292 | | - |
293 | | - filePath := fmt.Sprintf("openagent/resources/%s/%s/%s", category, userName, fileName) |
294 | | - |
295 | | - host := c.Ctx.Request.Host |
296 | | - origin := getOriginFromHost(host) |
297 | | - fileUrl, err := object.UploadFileToStorageSafe(filePath, fileBytes, origin, c.GetAcceptLanguage()) |
298 | | - if err != nil { |
299 | | - c.ResponseError(err.Error()) |
300 | | - return |
301 | | - } |
302 | | - |
303 | | - ext := strings.ToLower(filepath.Ext(fileName)) |
304 | | - mimeType := mime.TypeByExtension(ext) |
305 | | - fileTypeParts := strings.SplitN(mimeType, "/", 2) |
306 | | - detectedFileType := "unknown" |
307 | | - if len(fileTypeParts) > 0 { |
308 | | - detectedFileType = fileTypeParts[0] |
309 | | - } |
310 | | - |
311 | | - resource := object.NewResourceFromUpload("admin", userName, category, fileName, detectedFileType, ext, fileUrl, filePath, len(fileBytes), objectType, objectId) |
312 | | - _, err = object.AddResource(resource) |
313 | | - if err != nil { |
314 | | - c.ResponseError(err.Error()) |
315 | | - return |
316 | | - } |
317 | | - |
318 | | - c.ResponseOk(fileUrl) |
319 | | -} |
0 commit comments