file.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SiYuan - Build Your Eternal Digital Garden
  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. files = append(files, map[string]interface{}{
  158. "name": entry.Name(),
  159. "isDir": entry.IsDir(),
  160. })
  161. }
  162. ret.Data = files
  163. }
  164. func renameFile(c *gin.Context) {
  165. ret := gulu.Ret.NewResult()
  166. defer c.JSON(http.StatusOK, ret)
  167. arg, ok := util.JsonArg(c, ret)
  168. if !ok {
  169. c.JSON(http.StatusOK, ret)
  170. return
  171. }
  172. filePath := arg["path"].(string)
  173. filePath = filepath.Join(util.WorkspaceDir, filePath)
  174. _, err := os.Stat(filePath)
  175. if os.IsNotExist(err) {
  176. ret.Code = 404
  177. return
  178. }
  179. if nil != err {
  180. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  181. ret.Code = 500
  182. ret.Msg = err.Error()
  183. return
  184. }
  185. newPath := arg["newPath"].(string)
  186. newPath = filepath.Join(util.WorkspaceDir, newPath)
  187. if err = filelock.Rename(filePath, newPath); nil != err {
  188. logging.LogErrorf("rename file [%s] to [%s] failed: %s", filePath, newPath, err)
  189. ret.Code = 500
  190. ret.Msg = err.Error()
  191. return
  192. }
  193. }
  194. func removeFile(c *gin.Context) {
  195. ret := gulu.Ret.NewResult()
  196. defer c.JSON(http.StatusOK, ret)
  197. arg, ok := util.JsonArg(c, ret)
  198. if !ok {
  199. c.JSON(http.StatusOK, ret)
  200. return
  201. }
  202. filePath := arg["path"].(string)
  203. filePath = filepath.Join(util.WorkspaceDir, filePath)
  204. _, err := os.Stat(filePath)
  205. if os.IsNotExist(err) {
  206. ret.Code = 404
  207. return
  208. }
  209. if nil != err {
  210. logging.LogErrorf("stat [%s] failed: %s", filePath, err)
  211. ret.Code = 500
  212. ret.Msg = err.Error()
  213. return
  214. }
  215. if err = filelock.Remove(filePath); nil != err {
  216. logging.LogErrorf("remove [%s] failed: %s", filePath, err)
  217. ret.Code = 500
  218. ret.Msg = err.Error()
  219. return
  220. }
  221. }
  222. func putFile(c *gin.Context) {
  223. ret := gulu.Ret.NewResult()
  224. defer c.JSON(http.StatusOK, ret)
  225. filePath := c.PostForm("path")
  226. filePath = filepath.Join(util.WorkspaceDir, filePath)
  227. isDirStr := c.PostForm("isDir")
  228. isDir, _ := strconv.ParseBool(isDirStr)
  229. var err error
  230. if isDir {
  231. err = os.MkdirAll(filePath, 0755)
  232. if nil != err {
  233. logging.LogErrorf("make a dir [%s] failed: %s", filePath, err)
  234. }
  235. } else {
  236. fileHeader, _ := c.FormFile("file")
  237. if nil == fileHeader {
  238. logging.LogErrorf("form file is nil [path=%s]", filePath)
  239. ret.Code = 400
  240. ret.Msg = "form file is nil"
  241. return
  242. }
  243. for {
  244. dir := filepath.Dir(filePath)
  245. if err = os.MkdirAll(dir, 0755); nil != err {
  246. logging.LogErrorf("put a file [%s] make dir [%s] failed: %s", filePath, dir, err)
  247. break
  248. }
  249. var f multipart.File
  250. f, err = fileHeader.Open()
  251. if nil != err {
  252. logging.LogErrorf("open file failed: %s", err)
  253. break
  254. }
  255. var data []byte
  256. data, err = io.ReadAll(f)
  257. if nil != err {
  258. logging.LogErrorf("read file failed: %s", err)
  259. break
  260. }
  261. err = filelock.WriteFile(filePath, data)
  262. if nil != err {
  263. logging.LogErrorf("put a file [%s] failed: %s", filePath, err)
  264. break
  265. }
  266. break
  267. }
  268. }
  269. if nil != err {
  270. ret.Code = -1
  271. ret.Msg = err.Error()
  272. return
  273. }
  274. modTimeStr := c.PostForm("modTime")
  275. modTime := time.Now()
  276. if "" != modTimeStr {
  277. modTimeInt, parseErr := strconv.ParseInt(modTimeStr, 10, 64)
  278. if nil != parseErr {
  279. logging.LogErrorf("parse mod time [%s] failed: %s", modTimeStr, parseErr)
  280. ret.Code = 500
  281. ret.Msg = parseErr.Error()
  282. return
  283. }
  284. modTime = millisecond2Time(modTimeInt)
  285. }
  286. if err = os.Chtimes(filePath, modTime, modTime); nil != err {
  287. logging.LogErrorf("change time failed: %s", err)
  288. ret.Code = 500
  289. ret.Msg = err.Error()
  290. return
  291. }
  292. }
  293. func millisecond2Time(t int64) time.Time {
  294. sec := t / 1000
  295. msec := t % 1000
  296. return time.Unix(sec, msec*int64(time.Millisecond))
  297. }