wide/file/decompress.go

58 lines
1.4 KiB
Go
Raw Normal View History

// Copyright (c) 2014-2019, b3log.org & hacpai.com
2015-03-21 09:39:26 +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-03-21 09:39:26 +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 file
import (
"encoding/json"
"net/http"
"path/filepath"
"github.com/b3log/wide/util"
)
// DecompressHandler handles request of decompressing zip/tar.gz.
func DecompressHandler(w http.ResponseWriter, r *http.Request) {
2015-11-24 10:42:28 +03:00
result := util.NewResult()
defer util.RetResult(w, r, result)
2015-03-21 09:39:26 +03:00
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
2015-11-24 10:42:28 +03:00
result.Succ = false
2015-03-21 09:39:26 +03:00
return
}
path := args["path"].(string)
// base := filepath.Base(path)
dir := filepath.Dir(path)
if !util.File.IsExist(path) {
2015-11-24 10:42:28 +03:00
result.Succ = false
result.Msg = "Can't find file [" + path + "] to descompress"
2015-03-21 09:39:26 +03:00
return
}
err := util.Zip.Unzip(path, dir)
if nil != err {
logger.Error(err)
2015-11-24 10:42:28 +03:00
result.Succ = false
2015-03-21 09:39:26 +03:00
return
}
}