Files
artalkjs/server/handler/transfer_upload.go
qwqcode cf7725edba refactor(go): bump the module version and support go install (#979)
BREAKING CHANGE: The original Go module name was `github.com/ArtalkJS/Artalk`, which has now been modified to `github.com/artalkjs/artalk/v2` (all lowercase). This change may affect the CI pipeline or other areas if you are compiling the Artalk server and have specified the fixed Go module name. Regarding the original `-ldflags -X github.com/ArtalkJS/Artalk/internal/config.Version` and `CommitHash` definitions, please remove them, as they are now obsolete. The `Version` has been hardcoded into the Go code, and the `CommitHash` is now read from the Go build info via runtime/debug’s vcs.revision value. This change only affects the server developers and does not impact the frontend UI client.
2024-09-18 21:43:43 +08:00

70 lines
1.7 KiB
Go

package handler
import (
"io"
"os"
"github.com/artalkjs/artalk/v2/internal/core"
"github.com/artalkjs/artalk/v2/internal/log"
"github.com/artalkjs/artalk/v2/server/common"
"github.com/gofiber/fiber/v2"
)
type ResponseTransferUpload struct {
// The uploaded file name which can be used to import
Filename string `json:"filename"`
}
// @Id UploadArtrans
// @Summary Upload Artrans
// @Description Upload a file to prepare to import
// @Tags Transfer
// @Security ApiKeyAuth
// @Param file formData file true "Upload file in preparation for import task"
// @Accept mpfd
// @Produce json
// @Success 200 {object} ResponseTransferUpload{filename=string}
// @Failure 500 {object} Map{msg=string}
// @Router /transfer/upload [post]
func TransferUpload(app *core.App, router fiber.Router) {
router.Post("/transfer/upload", common.AdminGuard(app, func(c *fiber.Ctx) error {
// Get file from FormData
file, err := c.FormFile("file")
if err != nil {
log.Error(err)
return common.RespError(c, 500, "File read failed")
}
// Open file
src, err := file.Open()
if err != nil {
log.Error(err)
return common.RespError(c, 500, "File open failed")
}
defer src.Close()
// Read file to buffer
buf, err := io.ReadAll(src)
if err != nil {
log.Error(err)
return common.RespError(c, 500, "File read failed")
}
// Create temp file
tmpFile, err := os.CreateTemp("", "artalk-import-file-")
if err != nil {
log.Error(err)
return common.RespError(c, 500, "tmp file creation failed")
}
defer tmpFile.Close()
// Write buffer to temp file
tmpFile.Write(buf)
// Return filename
return common.RespData(c, ResponseTransferUpload{
Filename: tmpFile.Name(),
})
}))
}