2019-01-02 16:43:11 +03:00
|
|
|
// Copyright (c) 2014-2019, b3log.org & hacpai.com
|
2015-02-13 04:59:51 +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
|
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2015-02-13 04:59:51 +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.
|
|
|
|
|
|
|
|
package playground
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2015-02-13 05:50:14 +03:00
|
|
|
"io"
|
2015-02-13 04:59:51 +03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-02-16 12:10:21 +03:00
|
|
|
"strings"
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
"github.com/b3log/wide/conf"
|
2015-02-13 04:59:51 +03:00
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SaveHandler handles request of Playground code save.
|
|
|
|
func SaveHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 12:39:35 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
session, _ := session.HTTPSession.Get(r, session.CookieName)
|
2015-02-13 04:59:51 +03:00
|
|
|
if session.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := args["code"].(string)
|
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
// Step1. format code
|
|
|
|
cmd := exec.Command("gofmt")
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
stdin, err := cmd.StdinPipe()
|
2015-02-13 04:59:51 +03:00
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
io.WriteString(stdin, code)
|
|
|
|
stdin.Close()
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
bytes, _ := cmd.Output()
|
|
|
|
output := string(bytes)
|
2015-02-13 09:08:35 +03:00
|
|
|
if "" != output {
|
|
|
|
code = string(output)
|
2015-02-13 04:59:51 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-02-13 04:59:51 +03:00
|
|
|
data["code"] = code
|
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
// Step2. generate file name
|
|
|
|
hasher := md5.New()
|
2015-02-13 04:59:51 +03:00
|
|
|
hasher.Write([]byte(code))
|
2015-02-13 05:50:14 +03:00
|
|
|
fileName := hex.EncodeToString(hasher.Sum(nil))
|
2015-02-13 04:59:51 +03:00
|
|
|
fileName += ".go"
|
2015-02-13 05:50:14 +03:00
|
|
|
data["fileName"] = fileName
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2015-02-13 05:50:14 +03:00
|
|
|
// Step3. write file
|
2019-05-16 20:41:04 +03:00
|
|
|
filePath := filepath.Clean(conf.Wide.Data + "/playground" + fileName)
|
2015-02-13 05:50:14 +03:00
|
|
|
fout, err := os.Create(filePath)
|
2015-02-13 04:59:51 +03:00
|
|
|
fout.WriteString(code)
|
|
|
|
if err := fout.Close(); nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-02-13 04:59:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-02-16 12:10:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShortURLHandler handles request of short URL.
|
|
|
|
func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
|
2015-11-24 12:39:35 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2015-02-16 12:10:21 +03:00
|
|
|
|
2019-05-16 19:41:52 +03:00
|
|
|
session, _ := session.HTTPSession.Get(r, session.CookieName)
|
2015-02-16 12:10:21 +03:00
|
|
|
if session.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-02-16 12:10:21 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
url := args["url"].(string)
|
|
|
|
|
|
|
|
resp, _ := http.Post("http://dwz.cn/create.php", "application/x-www-form-urlencoded",
|
|
|
|
strings.NewReader("url="+url))
|
|
|
|
|
|
|
|
var response map[string]interface{}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2015-02-16 12:10:21 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shortURL := url
|
|
|
|
if 0 == response["status"].(float64) {
|
|
|
|
shortURL = response["tinyurl"].(string)
|
|
|
|
}
|
2015-02-13 04:59:51 +03:00
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Data = shortURL
|
2015-02-13 04:59:51 +03:00
|
|
|
}
|