file.go 7.9 KB

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