2018-05-10 11:08:49 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-05-11 23:49:02 +08:00
|
|
|
"fmt"
|
2018-05-10 11:08:49 +08:00
|
|
|
|
|
|
|
"github.com/goodrain/rainbond/api/handler"
|
2018-05-11 23:49:02 +08:00
|
|
|
httputil "github.com/goodrain/rainbond/util/http"
|
2018-05-10 11:08:49 +08:00
|
|
|
"github.com/goodrain/rainbond/api/model"
|
2018-05-10 17:45:23 +08:00
|
|
|
"github.com/goodrain/rainbond/db"
|
2018-05-14 14:30:32 +08:00
|
|
|
dbmodel "github.com/goodrain/rainbond/db/model"
|
|
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi"
|
2018-05-15 11:03:03 +08:00
|
|
|
"os"
|
2018-05-17 11:27:04 +08:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"io/ioutil"
|
2018-05-10 11:08:49 +08:00
|
|
|
)
|
|
|
|
|
2018-05-11 23:49:02 +08:00
|
|
|
type AppStruct struct {}
|
2018-05-10 11:08:49 +08:00
|
|
|
|
|
|
|
func (a *AppStruct) ExportApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2018-05-14 14:30:32 +08:00
|
|
|
switch r.Method{
|
|
|
|
case "POST":
|
|
|
|
var tr model.ExportAppStruct
|
|
|
|
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &tr.Body, nil)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-05-10 17:45:23 +08:00
|
|
|
|
2018-05-14 14:30:32 +08:00
|
|
|
if err := handler.GetAppHandler().Complete(&tr); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:12:42 +08:00
|
|
|
// 要先更新数据库再通知builder组件
|
2018-05-14 14:30:32 +08:00
|
|
|
app := model.NewAppStatusFrom(&tr)
|
2018-05-15 12:26:17 +08:00
|
|
|
db.GetManager().AppDao().DeleteModelByEventId(app.EventID)
|
2018-05-14 14:30:32 +08:00
|
|
|
if err := db.GetManager().AppDao().AddModel(app); err != nil {
|
2018-05-17 11:27:04 +08:00
|
|
|
httputil.ReturnError(r, w, 502, fmt.Sprintf("Failed to export app %s: %v", app.EventID, err))
|
2018-05-14 14:30:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-15 15:12:42 +08:00
|
|
|
err := handler.GetAppHandler().ExportApp(&tr)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 501, fmt.Sprintf("Failed to export app: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:30:32 +08:00
|
|
|
httputil.ReturnSuccess(r, w, nil)
|
|
|
|
case "GET":
|
|
|
|
eventId := strings.TrimSpace(chi.URLParam(r, "eventId"))
|
|
|
|
if eventId == "" {
|
|
|
|
httputil.ReturnError(r, w, 501, fmt.Sprintf("Arguments eventId is must defined."))
|
|
|
|
return
|
|
|
|
}
|
2018-05-10 11:08:49 +08:00
|
|
|
|
2018-05-14 14:30:32 +08:00
|
|
|
res, err := db.GetManager().AppDao().GetByEventId(eventId)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 502, fmt.Sprintf("Failed to query status of export app by event id %s: %v", eventId, err))
|
|
|
|
return
|
|
|
|
}
|
2018-05-11 23:49:02 +08:00
|
|
|
|
2018-05-17 16:10:44 +08:00
|
|
|
httputil.ReturnSuccess(r, w, res)
|
2018-05-10 17:45:23 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 11:08:49 +08:00
|
|
|
}
|
|
|
|
|
2018-05-15 11:03:03 +08:00
|
|
|
func (a *AppStruct) Download(w http.ResponseWriter, r *http.Request) {
|
2018-05-15 11:30:31 +08:00
|
|
|
format := strings.TrimSpace(chi.URLParam(r, "format"))
|
|
|
|
fileName := strings.TrimSpace(chi.URLParam(r, "fileName"))
|
2018-05-15 11:03:03 +08:00
|
|
|
tarFile := fmt.Sprintf("%s/%s/%s", handler.GetAppHandler().GetStaticDir(), format, fileName)
|
|
|
|
|
|
|
|
// return status code 502 if the file not exists.
|
2018-05-15 12:26:17 +08:00
|
|
|
if _, err := os.Stat(tarFile); os.IsNotExist(err) {
|
2018-05-15 11:03:03 +08:00
|
|
|
httputil.ReturnError(r, w, 502, fmt.Sprintf("Not found export app tar file: %s", tarFile))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeFile(w, r, tarFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppStruct) Upload(w http.ResponseWriter, r *http.Request) {
|
2018-05-17 11:27:04 +08:00
|
|
|
eventId := r.FormValue("eventId")
|
|
|
|
|
|
|
|
reader, header, err := r.FormFile("appTarFile")
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 501, "Failed to parse upload file.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
dirName := fmt.Sprintf("%s/import/%s", handler.GetAppHandler().GetStaticDir(), eventId)
|
|
|
|
os.MkdirAll(dirName, 0755)
|
|
|
|
|
|
|
|
fileName := fmt.Sprintf("%s/%s", dirName, header.Filename)
|
|
|
|
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 502, "Failed to open file: " + err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(file, reader); err != nil {
|
|
|
|
httputil.ReturnError(r, w, 503, "Failed to write file: " + err.Error())
|
|
|
|
}
|
|
|
|
httputil.ReturnSuccess(r, w, "Successful upload file.")
|
2018-05-15 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 11:08:49 +08:00
|
|
|
func (a *AppStruct) ImportApp(w http.ResponseWriter, r *http.Request) {
|
2018-05-17 11:27:04 +08:00
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
var app = dbmodel.AppStatus {
|
|
|
|
Format: "rainbond-app",
|
|
|
|
Status: "importing",
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &app, nil)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取tar包所在目录
|
|
|
|
sourceDir := fmt.Sprintf("%s/import/%s", handler.GetAppHandler().GetStaticDir(), app.EventID)
|
|
|
|
files, err := ioutil.ReadDir(sourceDir)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取tar包的不含后缀的文件名
|
|
|
|
name := files[0].Name()
|
|
|
|
ex := filepath.Ext(files[0].Name())
|
|
|
|
name = files[0].Name()[:len(name)-len(ex)]
|
|
|
|
|
|
|
|
// tar包解压后的目录
|
|
|
|
app.SourceDir = fmt.Sprintf("%s/%s", sourceDir, name)
|
|
|
|
|
|
|
|
// 要先更新数据库再通知builder组件
|
|
|
|
db.GetManager().AppDao().DeleteModelByEventId(app.EventID)
|
|
|
|
if err := db.GetManager().AppDao().AddModel(&app); err != nil {
|
|
|
|
httputil.ReturnError(r, w, 502, fmt.Sprintf("Failed to import app %s: %v", name, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.GetAppHandler().ImportApp(&app)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 501, fmt.Sprintf("Failed to import app: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, nil)
|
|
|
|
case "GET":
|
|
|
|
eventId := strings.TrimSpace(chi.URLParam(r, "eventId"))
|
|
|
|
if eventId == "" {
|
|
|
|
httputil.ReturnError(r, w, 501, fmt.Sprintf("Arguments eventId is must defined."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.GetManager().AppDao().GetByEventId(eventId)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ReturnError(r, w, 502, fmt.Sprintf("Failed to query status of export app by event id %s: %v", eventId, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.ReturnSuccess(r, w, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-10 11:08:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppStruct) BackupApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppStruct) RecoverApp(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//TODO
|
|
|
|
}
|