2018-01-01 07:35:13 +03:00
|
|
|
// Copyright (c) 2014-2018, b3log.org & hacpai.com
|
2014-11-13 17:28:28 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-11-13 17:28:28 +03:00
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2014-11-13 17:28:28 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-09-07 13:07:25 +04:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2014-09-07 14:13:55 +04:00
|
|
|
"os/exec"
|
2014-09-07 13:07:25 +04:00
|
|
|
|
2014-10-26 11:03:19 +03:00
|
|
|
"github.com/b3log/wide/conf"
|
2014-10-26 13:08:01 +03:00
|
|
|
"github.com/b3log/wide/session"
|
2014-09-07 13:07:25 +04:00
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
// GoFmtHandler handles request of formatting Go source code.
|
|
|
|
//
|
|
|
|
// This function will select a format tooll based on user's configuration:
|
2014-10-26 13:08:01 +03:00
|
|
|
// 1. gofmt
|
|
|
|
// 2. goimports
|
2014-09-07 14:13:55 +04:00
|
|
|
func GoFmtHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 10:42:28 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2014-09-07 14:13:55 +04:00
|
|
|
|
2014-10-26 13:08:01 +03:00
|
|
|
session, _ := session.HTTPSession.Get(r, "wide-session")
|
2014-11-26 08:49:27 +03:00
|
|
|
if session.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-10-26 13:08:01 +03:00
|
|
|
username := session.Values["username"].(string)
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
|
2014-10-26 13:08:01 +03:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = false
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := args["file"].(string)
|
|
|
|
|
2014-11-21 12:41:51 +03:00
|
|
|
if util.Go.IsAPI(filePath) {
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = false
|
2015-07-23 11:31:37 +03:00
|
|
|
|
2014-10-26 11:03:19 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-07 14:13:55 +04:00
|
|
|
fout, err := os.Create(filePath)
|
|
|
|
|
|
|
|
if nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = false
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
|
|
|
|
fout.WriteString(code)
|
|
|
|
if err := fout.Close(); nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = false
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
data := map[string]interface{}{}
|
2015-11-27 11:55:28 +03:00
|
|
|
result.Data = &data
|
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
data["code"] = code
|
|
|
|
|
|
|
|
result.Data = data
|
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
fmt := conf.GetGoFmt(username)
|
2014-10-26 13:08:01 +03:00
|
|
|
|
2014-09-07 14:13:55 +04:00
|
|
|
argv := []string{filePath}
|
2014-10-26 13:08:01 +03:00
|
|
|
cmd := exec.Command(fmt, argv...)
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
bytes, _ := cmd.Output()
|
|
|
|
output := string(bytes)
|
|
|
|
if "" == output {
|
2014-11-13 17:28:28 +03:00
|
|
|
// format error, returns the original content
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = true
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code = string(output)
|
2015-11-24 12:39:35 +03:00
|
|
|
data["code"] = code
|
|
|
|
|
2014-09-07 14:13:55 +04:00
|
|
|
fout, err = os.Create(filePath)
|
|
|
|
fout.WriteString(code)
|
|
|
|
if err := fout.Close(); nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2015-11-24 10:42:28 +03:00
|
|
|
result.Succ = false
|
2014-09-07 14:13:55 +04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|