mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
repo_model "github.com/hanzoai/git/models/repo"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/util"
|
|
"github.com/hanzoai/git/services/context"
|
|
"github.com/hanzoai/git/services/context/upload"
|
|
files_service "github.com/hanzoai/git/services/repository/files"
|
|
)
|
|
|
|
// UploadFileToServer upload file to server file dir not git
|
|
func UploadFileToServer(ctx *context.Context) {
|
|
file, header, err := ctx.Req.FormFile("file")
|
|
if err != nil {
|
|
ctx.ServerError("FormFile", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
buf := make([]byte, 1024)
|
|
n, _ := util.ReadAtMost(file, buf)
|
|
if n > 0 {
|
|
buf = buf[:n]
|
|
}
|
|
|
|
err = upload.Verify(buf, header.Filename, setting.Repository.Upload.AllowedTypes)
|
|
if err != nil {
|
|
ctx.HTTPError(http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
name := files_service.CleanGitTreePath(header.Filename)
|
|
if len(name) == 0 {
|
|
ctx.HTTPError(http.StatusBadRequest, "Upload file name is invalid")
|
|
return
|
|
}
|
|
|
|
// FIXME: need to check the file size according to setting.Repository.Upload.FileMaxSize
|
|
|
|
uploaded, err := repo_model.NewUpload(ctx, name, buf, file)
|
|
if err != nil {
|
|
ctx.ServerError("NewUpload", err)
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, map[string]string{"uuid": uploaded.UUID})
|
|
}
|
|
|
|
// RemoveUploadFileFromServer remove file from server file dir
|
|
func RemoveUploadFileFromServer(ctx *context.Context) {
|
|
fileUUID := ctx.FormString("file")
|
|
if err := repo_model.DeleteUploadByUUID(ctx, fileUUID); err != nil {
|
|
ctx.ServerError("DeleteUploadByUUID", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|