file.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. package api
  17. import (
  18. "io"
  19. "mime"
  20. "mime/multipart"
  21. "net/http"
  22. "os"
  23. "path/filepath"
  24. "strconv"
  25. "time"
  26. "github.com/88250/gulu"
  27. "github.com/gabriel-vasile/mimetype"
  28. "github.com/gin-gonic/gin"
  29. "github.com/siyuan-note/filelock"
  30. "github.com/siyuan-note/logging"
  31. "github.com/siyuan-note/siyuan/kernel/model"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func copyFile(c *gin.Context) {
  35. ret := gulu.Ret.NewResult()
  36. defer c.JSON(http.StatusOK, ret)
  37. arg, ok := util.JsonArg(c, ret)
  38. if !ok {
  39. return
  40. }
  41. src := arg["src"].(string)
  42. src, err := model.GetAssetAbsPath(src)
  43. if nil != err {
  44. logging.LogErrorf("get asset [%s] abs path failed: %s", src, err)
  45. ret.Code = -1
  46. ret.Msg = err.Error()
  47. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  48. return
  49. }
  50. info, err := os.Stat(src)
  51. if nil != err {
  52. logging.LogErrorf("stat [%s] failed: %s", src, err)
  53. ret.Code = -1
  54. ret.Msg = err.Error()
  55. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  56. return
  57. }
  58. if info.IsDir() {
  59. ret.Code = -1
  60. ret.Msg = "file is a directory"
  61. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  62. return
  63. }
  64. dest := arg["dest"].(string)
  65. if err = filelock.Copy(src, dest); nil != err {
  66. logging.LogErrorf("copy file [%s] to [%s] failed: %s", src, dest, err)
  67. ret.Code = -1
  68. ret.Msg = err.Error()
  69. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  70. return
  71. }
  72. }
  73. func getFile(c *gin.Context) {
  74. ret := gulu.Ret.NewResult()
  75. arg, ok := util.JsonArg(c, ret)
  76. if !ok {
  77. ret.Code = -1
  78. c.JSON(http.StatusAccepted, ret)
  79. return
  80. }
  81. filePath := arg["path"].(string)
  82. filePath = filepath.Join(util.WorkspaceDir, filePath)
  83. info, err := os.Stat(filePath)
  84. if os.IsNotExist(err) {
  85. ret.Code = 404
  86. c.JSON(http.StatusAccepted, ret)
  87. return
  88. }
  89. if nil != err {
  90. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  91. ret.Code = 500
  92. ret.Msg = err.Error()
  93. c.JSON(http.StatusAccepted, ret)
  94. return
  95. }
  96. if info.IsDir() {
  97. logging.LogErrorf("file [%s] is a directory", filePath)
  98. ret.Code = 405
  99. ret.Msg = "file is a directory"
  100. c.JSON(http.StatusAccepted, ret)
  101. return
  102. }
  103. data, err := filelock.ReadFile(filePath)
  104. if nil != err {
  105. logging.LogErrorf("read file [%s] failed: %s", filePath, err)
  106. ret.Code = 500
  107. ret.Msg = err.Error()
  108. c.JSON(http.StatusAccepted, ret)
  109. return
  110. }
  111. contentType := mime.TypeByExtension(filepath.Ext(filePath))
  112. if "" == contentType {
  113. if m := mimetype.Detect(data); nil != m {
  114. contentType = m.String()
  115. }
  116. }
  117. if "" == contentType {
  118. contentType = "application/octet-stream"
  119. }
  120. c.Data(http.StatusOK, contentType, data)
  121. }
  122. func readDir(c *gin.Context) {
  123. ret := gulu.Ret.NewResult()
  124. defer c.JSON(http.StatusOK, ret)
  125. arg, ok := util.JsonArg(c, ret)
  126. if !ok {
  127. c.JSON(http.StatusOK, ret)
  128. return
  129. }
  130. dirPath := arg["path"].(string)
  131. dirPath = filepath.Join(util.WorkspaceDir, dirPath)
  132. info, err := os.Stat(dirPath)
  133. if os.IsNotExist(err) {
  134. ret.Code = 404
  135. return
  136. }
  137. if nil != err {
  138. logging.LogErrorf("stat [%s] failed: %s", dirPath, err)
  139. ret.Code = 500
  140. ret.Msg = err.Error()
  141. return
  142. }
  143. if !info.IsDir() {
  144. logging.LogErrorf("file [%s] is not a directory", dirPath)
  145. ret.Code = 405
  146. ret.Msg = "file is not a directory"
  147. return
  148. }
  149. entries, err := os.ReadDir(dirPath)
  150. if nil != err {
  151. logging.LogErrorf("read dir [%s] failed: %s", dirPath, err)
  152. ret.Code = 500
  153. ret.Msg = err.Error()
  154. return
  155. }
  156. files := []map[string]interface{}{}
  157. for _, entry := range entries {
  158. path := filepath.Join(dirPath, entry.Name())
  159. info, err = os.Stat(path)
  160. if nil != err {
  161. logging.LogErrorf("stat [%s] failed: %s", path, err)
  162. ret.Code = 500
  163. ret.Msg = err.Error()
  164. return
  165. }
  166. files = append(files, map[string]interface{}{
  167. "name": entry.Name(),
  168. "isDir": info.IsDir(),
  169. "isSymlink": util.IsSymlink(entry),
  170. "updated": info.ModTime().Unix(),
  171. })
  172. }
  173. ret.Data = files
  174. }
  175. func renameFile(c *gin.Context) {
  176. ret := gulu.Ret.NewResult()
  177. defer c.JSON(http.StatusOK, ret)
  178. arg, ok := util.JsonArg(c, ret)
  179. if !ok {
  180. c.JSON(http.StatusOK, ret)
  181. return
  182. }
  183. filePath := arg["path"].(string)
  184. filePath = filepath.Join(util.WorkspaceDir, filePath)
  185. if !filelock.IsExist(filePath) {
  186. ret.Code = 404
  187. ret.Msg = "the [path] file or directory does not exist"
  188. return
  189. }
  190. newPath := arg["newPath"].(string)
  191. newPath = filepath.Join(util.WorkspaceDir, newPath)
  192. if filelock.IsExist(newPath) {
  193. ret.Code = 409
  194. ret.Msg = "the [newPath] file or directory already exists"
  195. return
  196. }
  197. if err := filelock.Rename(filePath, newPath); nil != err {
  198. logging.LogErrorf("rename file [%s] to [%s] failed: %s", filePath, newPath, err)
  199. ret.Code = 500
  200. ret.Msg = err.Error()
  201. return
  202. }
  203. }
  204. func removeFile(c *gin.Context) {
  205. ret := gulu.Ret.NewResult()
  206. defer c.JSON(http.StatusOK, ret)
  207. arg, ok := util.JsonArg(c, ret)
  208. if !ok {
  209. c.JSON(http.StatusOK, ret)
  210. return
  211. }
  212. filePath := arg["path"].(string)
  213. filePath = filepath.Join(util.WorkspaceDir, filePath)
  214. _, err := os.Stat(filePath)
  215. if os.IsNotExist(err) {
  216. ret.Code = 404
  217. return
  218. }
  219. if nil != err {
  220. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  221. ret.Code = 500
  222. ret.Msg = err.Error()
  223. return
  224. }
  225. if err = filelock.Remove(filePath); nil != err {
  226. logging.LogErrorf("remove [%s] failed: %s", filePath, err)
  227. ret.Code = 500
  228. ret.Msg = err.Error()
  229. return
  230. }
  231. }
  232. func putFile(c *gin.Context) {
  233. ret := gulu.Ret.NewResult()
  234. defer c.JSON(http.StatusOK, ret)
  235. filePath := c.PostForm("path")
  236. filePath = filepath.Join(util.WorkspaceDir, filePath)
  237. isDirStr := c.PostForm("isDir")
  238. isDir, _ := strconv.ParseBool(isDirStr)
  239. var err error
  240. if isDir {
  241. err = os.MkdirAll(filePath, 0755)
  242. if nil != err {
  243. logging.LogErrorf("make a dir [%s] failed: %s", filePath, err)
  244. }
  245. } else {
  246. fileHeader, _ := c.FormFile("file")
  247. if nil == fileHeader {
  248. logging.LogErrorf("form file is nil [path=%s]", filePath)
  249. ret.Code = 400
  250. ret.Msg = "form file is nil"
  251. return
  252. }
  253. for {
  254. dir := filepath.Dir(filePath)
  255. if err = os.MkdirAll(dir, 0755); nil != err {
  256. logging.LogErrorf("put a file [%s] make dir [%s] failed: %s", filePath, dir, err)
  257. break
  258. }
  259. var f multipart.File
  260. f, err = fileHeader.Open()
  261. if nil != err {
  262. logging.LogErrorf("open file failed: %s", err)
  263. break
  264. }
  265. var data []byte
  266. data, err = io.ReadAll(f)
  267. if nil != err {
  268. logging.LogErrorf("read file failed: %s", err)
  269. break
  270. }
  271. err = filelock.WriteFile(filePath, data)
  272. if nil != err {
  273. logging.LogErrorf("put a file [%s] failed: %s", filePath, err)
  274. break
  275. }
  276. break
  277. }
  278. }
  279. if nil != err {
  280. ret.Code = -1
  281. ret.Msg = err.Error()
  282. return
  283. }
  284. modTimeStr := c.PostForm("modTime")
  285. modTime := time.Now()
  286. if "" != modTimeStr {
  287. modTimeInt, parseErr := strconv.ParseInt(modTimeStr, 10, 64)
  288. if nil != parseErr {
  289. logging.LogErrorf("parse mod time [%s] failed: %s", modTimeStr, parseErr)
  290. ret.Code = 500
  291. ret.Msg = parseErr.Error()
  292. return
  293. }
  294. modTime = millisecond2Time(modTimeInt)
  295. }
  296. if err = os.Chtimes(filePath, modTime, modTime); nil != err {
  297. logging.LogErrorf("change time failed: %s", err)
  298. ret.Code = 500
  299. ret.Msg = err.Error()
  300. return
  301. }
  302. }
  303. func millisecond2Time(t int64) time.Time {
  304. sec := t / 1000
  305. msec := t % 1000
  306. return time.Unix(sec, msec*int64(time.Millisecond))
  307. }