file.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. _, err := os.Stat(filePath)
  186. if os.IsNotExist(err) {
  187. ret.Code = 404
  188. ret.Msg = err.Error()
  189. return
  190. }
  191. if nil != err {
  192. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  193. ret.Code = 500
  194. ret.Msg = err.Error()
  195. return
  196. }
  197. newPath := arg["newPath"].(string)
  198. newPath = filepath.Join(util.WorkspaceDir, newPath)
  199. if gulu.File.IsExist(newPath) {
  200. ret.Code = 409
  201. ret.Msg = "the [newPath] file or directory already exists"
  202. return
  203. }
  204. if err = filelock.Rename(filePath, newPath); nil != err {
  205. logging.LogErrorf("rename file [%s] to [%s] failed: %s", filePath, newPath, err)
  206. ret.Code = 500
  207. ret.Msg = err.Error()
  208. return
  209. }
  210. }
  211. func removeFile(c *gin.Context) {
  212. ret := gulu.Ret.NewResult()
  213. defer c.JSON(http.StatusOK, ret)
  214. arg, ok := util.JsonArg(c, ret)
  215. if !ok {
  216. c.JSON(http.StatusOK, ret)
  217. return
  218. }
  219. filePath := arg["path"].(string)
  220. filePath = filepath.Join(util.WorkspaceDir, filePath)
  221. _, err := os.Stat(filePath)
  222. if os.IsNotExist(err) {
  223. ret.Code = 404
  224. return
  225. }
  226. if nil != err {
  227. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  228. ret.Code = 500
  229. ret.Msg = err.Error()
  230. return
  231. }
  232. if err = filelock.Remove(filePath); nil != err {
  233. logging.LogErrorf("remove [%s] failed: %s", filePath, err)
  234. ret.Code = 500
  235. ret.Msg = err.Error()
  236. return
  237. }
  238. }
  239. func putFile(c *gin.Context) {
  240. ret := gulu.Ret.NewResult()
  241. defer c.JSON(http.StatusOK, ret)
  242. filePath := c.PostForm("path")
  243. filePath = filepath.Join(util.WorkspaceDir, filePath)
  244. isDirStr := c.PostForm("isDir")
  245. isDir, _ := strconv.ParseBool(isDirStr)
  246. var err error
  247. if isDir {
  248. err = os.MkdirAll(filePath, 0755)
  249. if nil != err {
  250. logging.LogErrorf("make a dir [%s] failed: %s", filePath, err)
  251. }
  252. } else {
  253. fileHeader, _ := c.FormFile("file")
  254. if nil == fileHeader {
  255. logging.LogErrorf("form file is nil [path=%s]", filePath)
  256. ret.Code = 400
  257. ret.Msg = "form file is nil"
  258. return
  259. }
  260. for {
  261. dir := filepath.Dir(filePath)
  262. if err = os.MkdirAll(dir, 0755); nil != err {
  263. logging.LogErrorf("put a file [%s] make dir [%s] failed: %s", filePath, dir, err)
  264. break
  265. }
  266. var f multipart.File
  267. f, err = fileHeader.Open()
  268. if nil != err {
  269. logging.LogErrorf("open file failed: %s", err)
  270. break
  271. }
  272. var data []byte
  273. data, err = io.ReadAll(f)
  274. if nil != err {
  275. logging.LogErrorf("read file failed: %s", err)
  276. break
  277. }
  278. err = filelock.WriteFile(filePath, data)
  279. if nil != err {
  280. logging.LogErrorf("put a file [%s] failed: %s", filePath, err)
  281. break
  282. }
  283. break
  284. }
  285. }
  286. if nil != err {
  287. ret.Code = -1
  288. ret.Msg = err.Error()
  289. return
  290. }
  291. modTimeStr := c.PostForm("modTime")
  292. modTime := time.Now()
  293. if "" != modTimeStr {
  294. modTimeInt, parseErr := strconv.ParseInt(modTimeStr, 10, 64)
  295. if nil != parseErr {
  296. logging.LogErrorf("parse mod time [%s] failed: %s", modTimeStr, parseErr)
  297. ret.Code = 500
  298. ret.Msg = parseErr.Error()
  299. return
  300. }
  301. modTime = millisecond2Time(modTimeInt)
  302. }
  303. if err = os.Chtimes(filePath, modTime, modTime); nil != err {
  304. logging.LogErrorf("change time failed: %s", err)
  305. ret.Code = 500
  306. ret.Msg = err.Error()
  307. return
  308. }
  309. }
  310. func millisecond2Time(t int64) time.Time {
  311. sec := t / 1000
  312. msec := t % 1000
  313. return time.Unix(sec, msec*int64(time.Millisecond))
  314. }