A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1// SiYuan - Refactor your thinking
2// Copyright (c) 2020-present, b3log.org
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package api
18
19import (
20 "net/http"
21 "path/filepath"
22
23 "github.com/88250/gulu"
24 "github.com/gin-gonic/gin"
25 "github.com/siyuan-note/siyuan/kernel/util"
26)
27
28func zip(c *gin.Context) {
29 ret := gulu.Ret.NewResult()
30 defer c.JSON(http.StatusOK, ret)
31
32 arg, ok := util.JsonArg(c, ret)
33 if !ok {
34 return
35 }
36
37 entryPath := arg["path"].(string)
38 entryAbsPath, err := util.GetAbsPathInWorkspace(entryPath)
39 if err != nil {
40 ret.Code = -1
41 ret.Msg = err.Error()
42 return
43 }
44
45 zipFilePath := arg["zipPath"].(string)
46 zipAbsFilePath, err := util.GetAbsPathInWorkspace(zipFilePath)
47 if err != nil {
48 ret.Code = -1
49 ret.Msg = err.Error()
50 return
51 }
52
53 zipFile, err := gulu.Zip.Create(zipAbsFilePath)
54 if err != nil {
55 ret.Code = -1
56 ret.Msg = err.Error()
57 return
58 }
59
60 base := filepath.Base(entryAbsPath)
61 if gulu.File.IsDir(entryAbsPath) {
62 err = zipFile.AddDirectory(base, entryAbsPath)
63 } else {
64 err = zipFile.AddEntry(base, entryAbsPath)
65 }
66 if err != nil {
67 ret.Code = -1
68 ret.Msg = err.Error()
69 return
70 }
71
72 if err = zipFile.Close(); err != nil {
73 ret.Code = -1
74 ret.Msg = err.Error()
75 return
76 }
77}
78
79func unzip(c *gin.Context) {
80 ret := gulu.Ret.NewResult()
81 defer c.JSON(http.StatusOK, ret)
82
83 arg, ok := util.JsonArg(c, ret)
84 if !ok {
85 return
86 }
87
88 zipFilePath := arg["zipPath"].(string)
89 zipAbsFilePath, err := util.GetAbsPathInWorkspace(zipFilePath)
90 if err != nil {
91 ret.Code = -1
92 ret.Msg = err.Error()
93 return
94 }
95
96 entryPath := arg["path"].(string)
97 entryAbsPath, err := util.GetAbsPathInWorkspace(entryPath)
98 if err != nil {
99 ret.Code = -1
100 ret.Msg = err.Error()
101 return
102 }
103
104 if err := gulu.Zip.Unzip(zipAbsFilePath, entryAbsPath); err != nil {
105 ret.Code = -1
106 ret.Msg = err.Error()
107 return
108 }
109}