2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
2014-11-28 18:12:38 +03:00
|
|
|
//
|
2014-11-24 12:24:35 +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-28 18:12:38 +03:00
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2014-11-28 18:12:38 +03:00
|
|
|
//
|
2014-11-24 12:24:35 +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-11-24 05:39:33 +03:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2014-11-24 12:20:43 +03:00
|
|
|
"os"
|
2014-11-24 05:39:33 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
2019-05-24 16:04:25 +03:00
|
|
|
"github.com/b3log/gulu"
|
2014-11-24 05:39:33 +03:00
|
|
|
)
|
|
|
|
|
2015-03-09 09:16:46 +03:00
|
|
|
// GetZipHandler handles request of retrieving zip file.
|
|
|
|
func GetZipHandler(w http.ResponseWriter, r *http.Request) {
|
2014-11-24 06:17:42 +03:00
|
|
|
q := r.URL.Query()
|
|
|
|
path := q["path"][0]
|
|
|
|
|
|
|
|
if ".zip" != filepath.Ext(path) {
|
|
|
|
http.Error(w, "Bad Request", 400)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-24 16:04:25 +03:00
|
|
|
if !gulu.File.IsExist(path) {
|
2014-11-24 06:17:42 +03:00
|
|
|
http.Error(w, "Not Found", 404)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-24 12:20:43 +03:00
|
|
|
filename := filepath.Base(path)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
|
2014-12-18 17:50:24 +03:00
|
|
|
w.Header().Set("Content-Type", "application/zip")
|
2014-11-24 06:17:42 +03:00
|
|
|
http.ServeFile(w, r, path)
|
2014-11-24 12:20:43 +03:00
|
|
|
|
|
|
|
os.Remove(path)
|
2014-11-24 06:17:42 +03:00
|
|
|
}
|
|
|
|
|
2015-03-09 09:16:46 +03:00
|
|
|
// CreateZipHandler handles request of creating zip.
|
|
|
|
func CreateZipHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-24 16:04:25 +03:00
|
|
|
data := gulu.Ret.NewResult()
|
|
|
|
defer gulu.Ret.RetResult(w, r, data)
|
2014-11-24 05:39:33 +03:00
|
|
|
|
|
|
|
var args map[string]interface{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
data.Code = -1
|
2014-11-24 05:39:33 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path := args["path"].(string)
|
2015-08-04 16:37:14 +03:00
|
|
|
var name string
|
|
|
|
|
2014-11-24 05:39:33 +03:00
|
|
|
base := filepath.Base(path)
|
|
|
|
|
2015-08-04 16:37:14 +03:00
|
|
|
if nil != args["name"] {
|
|
|
|
name = args["name"].(string)
|
|
|
|
} else {
|
|
|
|
name = base
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
|
2019-05-24 16:04:25 +03:00
|
|
|
if !gulu.File.IsExist(path) {
|
2019-05-24 16:52:17 +03:00
|
|
|
data.Code = -1
|
2015-11-24 10:42:28 +03:00
|
|
|
data.Msg = "Can't find file [" + path + "]"
|
2014-11-24 05:39:33 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:37:14 +03:00
|
|
|
zipPath := filepath.Join(dir, name)
|
2019-05-24 16:04:25 +03:00
|
|
|
zipFile, err := gulu.Zip.Create(zipPath + ".zip")
|
2014-11-24 05:39:33 +03:00
|
|
|
if nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2019-05-24 16:52:17 +03:00
|
|
|
data.Code = -1
|
2014-11-24 05:39:33 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer zipFile.Close()
|
|
|
|
|
2019-05-24 16:04:25 +03:00
|
|
|
if gulu.File.IsDir(path) {
|
2014-11-24 05:39:33 +03:00
|
|
|
zipFile.AddDirectory(base, path)
|
|
|
|
} else {
|
|
|
|
zipFile.AddEntry(base, path)
|
|
|
|
}
|
2015-08-04 16:37:14 +03:00
|
|
|
|
2015-11-24 10:42:28 +03:00
|
|
|
data.Data = zipPath
|
2014-11-24 05:39:33 +03:00
|
|
|
}
|