mirror of https://gitee.com/godoos/godoos.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.2 KiB
92 lines
2.2 KiB
package store
|
|
|
|
import (
|
|
"godo/files"
|
|
"godo/libs"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// UploadHandler 处理上传的HTTP请求
|
|
func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// 解析上传的文件
|
|
err := r.ParseMultipartForm(10000 << 20) // 限制最大上传大小为1000MB
|
|
if err != nil {
|
|
http.Error(w, "上传文件过大"+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
file, header, err := r.FormFile("files") // 表单字段名为"files"
|
|
if err != nil {
|
|
http.Error(w, "没有找到文件", http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
// 读取文件内容
|
|
fileBytes, err := io.ReadAll(file)
|
|
if err != nil {
|
|
log.Printf("读取文件内容出错: %v", err)
|
|
http.Error(w, "读取文件内容出错", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cachePath := libs.GetCacheDir()
|
|
baseName := filepath.Base(header.Filename)
|
|
|
|
savePath := filepath.Join(cachePath, baseName)
|
|
|
|
out, err := os.Create(savePath)
|
|
if err != nil {
|
|
log.Printf("创建文件出错: %v", err)
|
|
http.Error(w, "保存文件出错", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer out.Close()
|
|
|
|
// 将文件内容写入到服务器上的文件
|
|
_, err = out.Write(fileBytes)
|
|
if err != nil {
|
|
log.Printf("写入文件出错: %v", err)
|
|
http.Error(w, "写入文件出错", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
zipPath, err := files.Decompress(savePath, cachePath)
|
|
if err != nil {
|
|
libs.ErrorMsg(w, err.Error())
|
|
return
|
|
}
|
|
storeFile := filepath.Join(zipPath, "install.json")
|
|
if !libs.PathExists(storeFile) {
|
|
libs.ErrorMsg(w, "store.json not found")
|
|
return
|
|
}
|
|
installCacheInfo, err := GetInstallInfoByPath(storeFile)
|
|
if err != nil {
|
|
libs.ErrorMsg(w, err.Error())
|
|
return
|
|
}
|
|
runDir := libs.GetRunDir()
|
|
targetDir := filepath.Join(runDir, installCacheInfo.Name)
|
|
err = files.CopyResource(zipPath, targetDir)
|
|
if err != nil {
|
|
libs.ErrorMsg(w, err.Error())
|
|
return
|
|
}
|
|
err = os.RemoveAll(zipPath)
|
|
if err != nil {
|
|
libs.ErrorMsg(w, err.Error())
|
|
return
|
|
}
|
|
|
|
installInfo, err := Installation(installCacheInfo.Name)
|
|
if err != nil {
|
|
libs.ErrorData(w, installInfo, "the install.json is error:"+err.Error())
|
|
return
|
|
}
|
|
libs.SuccessMsg(w, installInfo, "install the app success!")
|
|
}
|
|
|