file_system.go 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "math"
  11. "mime/multipart"
  12. "net/http"
  13. "net/url"
  14. "os"
  15. "path/filepath"
  16. "runtime"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "github.com/gorilla/websocket"
  22. uuid "github.com/satori/go.uuid"
  23. fs "imuslab.com/arozos/mod/filesystem"
  24. fsp "imuslab.com/arozos/mod/filesystem/fspermission"
  25. hidden "imuslab.com/arozos/mod/filesystem/hidden"
  26. metadata "imuslab.com/arozos/mod/filesystem/metadata"
  27. module "imuslab.com/arozos/mod/modules"
  28. prout "imuslab.com/arozos/mod/prouter"
  29. "imuslab.com/arozos/mod/share"
  30. storage "imuslab.com/arozos/mod/storage"
  31. user "imuslab.com/arozos/mod/user"
  32. )
  33. var (
  34. thumbRenderHandler *metadata.RenderHandler
  35. shareManager *share.Manager
  36. )
  37. type trashedFile struct {
  38. Filename string
  39. Filepath string
  40. FileExt string
  41. IsDir bool
  42. Filesize int64
  43. RemoveTimestamp int64
  44. RemoveDate string
  45. OriginalPath string
  46. OriginalFilename string
  47. }
  48. func FileSystemInit() {
  49. router := prout.NewModuleRouter(prout.RouterOption{
  50. ModuleName: "File Manager",
  51. AdminOnly: false,
  52. UserHandler: userHandler,
  53. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  54. sendErrorResponse(w, "Permission Denied")
  55. },
  56. })
  57. //Upload related functions
  58. router.HandleFunc("/system/file_system/upload", system_fs_handleUpload)
  59. router.HandleFunc("/system/file_system/lowmemUpload", system_fs_handleLowMemoryUpload)
  60. //Other file operations
  61. router.HandleFunc("/system/file_system/validateFileOpr", system_fs_validateFileOpr)
  62. router.HandleFunc("/system/file_system/fileOpr", system_fs_handleOpr)
  63. router.HandleFunc("/system/file_system/ws/fileOpr", system_fs_handleWebSocketOpr)
  64. router.HandleFunc("/system/file_system/listDir", system_fs_handleList)
  65. router.HandleFunc("/system/file_system/listDirHash", system_fs_handleDirHash)
  66. router.HandleFunc("/system/file_system/listRoots", system_fs_listRoot)
  67. router.HandleFunc("/system/file_system/listDrives", system_fs_listDrives)
  68. router.HandleFunc("/system/file_system/newItem", system_fs_handleNewObjects)
  69. router.HandleFunc("/system/file_system/preference", system_fs_handleUserPreference)
  70. router.HandleFunc("/system/file_system/listTrash", system_fs_scanTrashBin)
  71. router.HandleFunc("/system/file_system/ws/listTrash", system_fs_WebSocketScanTrashBin)
  72. router.HandleFunc("/system/file_system/clearTrash", system_fs_clearTrashBin)
  73. router.HandleFunc("/system/file_system/restoreTrash", system_fs_restoreFile)
  74. router.HandleFunc("/system/file_system/zipHandler", system_fs_zipHandler)
  75. router.HandleFunc("/system/file_system/getProperties", system_fs_getFileProperties)
  76. router.HandleFunc("/system/file_system/pathTranslate", system_fs_handlePathTranslate)
  77. router.HandleFunc("/system/file_system/handleFileWrite", system_fs_handleFileWrite)
  78. router.HandleFunc("/system/file_system/handleFilePermission", system_fs_handleFilePermission)
  79. router.HandleFunc("/system/file_system/search", system_fs_handleFileSearch)
  80. //Thumbnail caching functions
  81. router.HandleFunc("/system/file_system/handleFolderCache", system_fs_handleFolderCache)
  82. router.HandleFunc("/system/file_system/handleCacheRender", system_fs_handleCacheRender)
  83. //Register the module
  84. moduleHandler.RegisterModule(module.ModuleInfo{
  85. Name: "File Manager",
  86. Group: "System Tools",
  87. IconPath: "SystemAO/file_system/img/small_icon.png",
  88. Version: "1.0",
  89. StartDir: "SystemAO/file_system/file_explorer.html",
  90. SupportFW: true,
  91. InitFWSize: []int{1080, 580},
  92. LaunchFWDir: "SystemAO/file_system/file_explorer.html",
  93. SupportEmb: false,
  94. })
  95. //Register the Trashbin module
  96. moduleHandler.RegisterModule(module.ModuleInfo{
  97. Name: "Trash Bin",
  98. Group: "System Tools",
  99. IconPath: "SystemAO/file_system/trashbin_img/small_icon.png",
  100. Version: "1.0",
  101. StartDir: "SystemAO/file_system/trashbin.html",
  102. SupportFW: true,
  103. InitFWSize: []int{1080, 580},
  104. LaunchFWDir: "SystemAO/file_system/trashbin.html",
  105. SupportEmb: false,
  106. SupportedExt: []string{"*"},
  107. })
  108. //Register the Zip Extractor module
  109. moduleHandler.RegisterModule(module.ModuleInfo{
  110. Name: "Zip Extractor",
  111. Group: "System Tools",
  112. IconPath: "SystemAO/file_system/img/zip_extractor.png",
  113. Version: "1.0",
  114. SupportFW: false,
  115. LaunchEmb: "SystemAO/file_system/zip_extractor.html",
  116. SupportEmb: true,
  117. InitEmbSize: []int{260, 120},
  118. SupportedExt: []string{".zip"},
  119. })
  120. //Create user root if not exists
  121. err := os.MkdirAll(*root_directory+"users/", 0755)
  122. if err != nil {
  123. log.Println("Failed to create system storage root.")
  124. panic(err)
  125. }
  126. //Create database table if not exists
  127. err = sysdb.NewTable("fs")
  128. if err != nil {
  129. log.Println("Failed to create table for file system")
  130. panic(err)
  131. }
  132. //Create a RenderHandler for caching thumbnails
  133. thumbRenderHandler = metadata.NewRenderHandler()
  134. /*
  135. Share Related Registering
  136. This section of functions create and register the file share service
  137. for the arozos
  138. */
  139. //Create a share manager to handle user file sharae
  140. shareManager = share.NewShareManager(share.Options{
  141. AuthAgent: authAgent,
  142. Database: sysdb,
  143. UserHandler: userHandler,
  144. HostName: *host_name,
  145. TmpFolder: *tmp_directory,
  146. })
  147. //Share related functions
  148. router.HandleFunc("/system/file_system/share/new", shareManager.HandleCreateNewShare)
  149. router.HandleFunc("/system/file_system/share/delete", shareManager.HandleDeleteShare)
  150. router.HandleFunc("/system/file_system/share/edit", shareManager.HandleEditShare)
  151. //Handle the main share function
  152. http.HandleFunc("/share", shareManager.HandleShareAccess)
  153. /*
  154. Nighly Tasks
  155. These functions allow file system to clear and maintain
  156. the arozos file system when no one is using the system
  157. */
  158. //Clear tmp folder if files is placed here too long
  159. RegisterNightlyTask(system_fs_clearOldTmpFiles)
  160. //Clear shares that its parent file no longer exists in the system
  161. shareManager.ValidateAndClearShares()
  162. RegisterNightlyTask(shareManager.ValidateAndClearShares)
  163. }
  164. /*
  165. File Search
  166. Handle file search in wildcard and recursive search
  167. */
  168. func system_fs_handleFileSearch(w http.ResponseWriter, r *http.Request) {
  169. //Get the user information
  170. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  171. if err != nil {
  172. sendErrorResponse(w, "User not logged in")
  173. return
  174. }
  175. //Get the search target root path
  176. vpath, err := mv(r, "path", true)
  177. if err != nil {
  178. sendErrorResponse(w, "Invalid vpath given")
  179. return
  180. }
  181. keyword, err := mv(r, "keyword", true)
  182. if err != nil {
  183. sendErrorResponse(w, "Invalid keyword given")
  184. return
  185. }
  186. //Translate the vpath to realpath
  187. rpath, err := userinfo.VirtualPathToRealPath(vpath)
  188. if err != nil {
  189. sendErrorResponse(w, "Invalid path given")
  190. return
  191. }
  192. //Check if the search mode is recursive keyword or wildcard
  193. if len(keyword) > 1 && keyword[:1] == "/" {
  194. //Wildcard
  195. wildcard := keyword[1:]
  196. matchingFiles, err := filepath.Glob(filepath.Join(rpath, wildcard))
  197. if err != nil {
  198. sendErrorResponse(w, err.Error())
  199. return
  200. }
  201. //Prepare result struct
  202. results := []fs.FileData{}
  203. //Process the matching files. Do not allow directory escape
  204. srcAbs, _ := filepath.Abs(rpath)
  205. srcAbs = filepath.ToSlash(srcAbs)
  206. escaped := false
  207. for _, matchedFile := range matchingFiles {
  208. absMatch, _ := filepath.Abs(matchedFile)
  209. absMatch = filepath.ToSlash(absMatch)
  210. if !strings.Contains(absMatch, srcAbs) {
  211. escaped = true
  212. }
  213. thisVpath, _ := userinfo.RealPathToVirtualPath(matchedFile)
  214. results = append(results, fs.GetFileDataFromPath(thisVpath, matchedFile, 2))
  215. }
  216. if escaped {
  217. sendErrorResponse(w, "Search keywords contain escape character!")
  218. return
  219. }
  220. //OK. Tidy up the results
  221. js, _ := json.Marshal(results)
  222. sendJSONResponse(w, string(js))
  223. } else {
  224. //Recursive keyword
  225. results := []fs.FileData{}
  226. err := filepath.Walk(rpath, func(path string, info os.FileInfo, err error) error {
  227. if strings.Contains(filepath.Base(path), keyword) {
  228. //This is a matching file
  229. if !fs.IsInsideHiddenFolder(path) {
  230. thisVpath, _ := userinfo.RealPathToVirtualPath(path)
  231. results = append(results, fs.GetFileDataFromPath(thisVpath, path, 2))
  232. }
  233. }
  234. return nil
  235. })
  236. if err != nil {
  237. sendErrorResponse(w, err.Error())
  238. return
  239. }
  240. //OK. Tidy up the results
  241. js, _ := json.Marshal(results)
  242. sendJSONResponse(w, string(js))
  243. }
  244. }
  245. /*
  246. Handle low-memory upload operations
  247. This function is specailly designed to work with low memory devices
  248. (e.g. ZeroPi / Orange Pi Zero with 512MB RAM)
  249. */
  250. func system_fs_handleLowMemoryUpload(w http.ResponseWriter, r *http.Request) {
  251. //Get user info
  252. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  253. if err != nil {
  254. w.WriteHeader(http.StatusUnauthorized)
  255. w.Write([]byte("401 - Unauthorized"))
  256. return
  257. }
  258. //Get filename and upload path
  259. filename, err := mv(r, "filename", false)
  260. if filename == "" || err != nil {
  261. w.WriteHeader(http.StatusInternalServerError)
  262. w.Write([]byte("500 - Invalid filename given"))
  263. return
  264. }
  265. //Get upload target directory
  266. uploadTarget, err := mv(r, "path", false)
  267. if uploadTarget == "" || err != nil {
  268. w.WriteHeader(http.StatusInternalServerError)
  269. w.Write([]byte("500 - Invalid path given"))
  270. return
  271. }
  272. //Check if the user can write to this folder
  273. if !userinfo.CanWrite(uploadTarget) {
  274. //No permission
  275. w.WriteHeader(http.StatusForbidden)
  276. w.Write([]byte("403 - Access Denied"))
  277. return
  278. }
  279. //Translate the upload target directory
  280. realUploadPath, err := userinfo.VirtualPathToRealPath(uploadTarget)
  281. if err != nil {
  282. w.WriteHeader(http.StatusInternalServerError)
  283. w.Write([]byte("500 - Path translation failed"))
  284. return
  285. }
  286. //Generate an UUID for this upload
  287. uploadUUID := uuid.NewV4().String()
  288. uploadFolder := filepath.Join(*tmp_directory, "uploads", uploadUUID)
  289. os.MkdirAll(uploadFolder, 0700)
  290. targetUploadLocation := filepath.Join(realUploadPath, filename)
  291. if !fileExists(realUploadPath) {
  292. os.MkdirAll(realUploadPath, 0755)
  293. }
  294. //Start websocket connection
  295. var upgrader = websocket.Upgrader{}
  296. c, err := upgrader.Upgrade(w, r, nil)
  297. defer c.Close()
  298. //Handle WebSocket upload
  299. blockCounter := 0
  300. chunkName := []string{}
  301. lastChunkArrivalTime := time.Now().Unix()
  302. //Setup a timeout listener, check if connection still active every 1 minute
  303. ticker := time.NewTicker(60 * time.Second)
  304. done := make(chan bool)
  305. go func() {
  306. for {
  307. select {
  308. case <-done:
  309. return
  310. case <-ticker.C:
  311. if time.Now().Unix()-lastChunkArrivalTime > 300 {
  312. //Already 5 minutes without new data arraival. Stop connection
  313. log.Println("Upload WebSocket connection timeout. Disconnecting.")
  314. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  315. time.Sleep(1 * time.Second)
  316. c.Close()
  317. return
  318. }
  319. }
  320. }
  321. }()
  322. totalFileSize := int64(0)
  323. for {
  324. mt, message, err := c.ReadMessage()
  325. if err != nil {
  326. //Connection closed by client. Clear the tmp folder and exit
  327. log.Println("Upload terminated by client. Cleaning tmp folder.")
  328. //Clear the tmp folder
  329. time.Sleep(1 * time.Second)
  330. os.RemoveAll(uploadFolder)
  331. return
  332. }
  333. //The mt should be 2 = binary for file upload and 1 for control syntax
  334. if mt == 1 {
  335. msg := strings.TrimSpace(string(message))
  336. if msg == "done" {
  337. //Start the merging process
  338. log.Println(userinfo.Username + " uploaded a file: " + filepath.Base(targetUploadLocation))
  339. break
  340. } else {
  341. //Unknown operations
  342. }
  343. } else if mt == 2 {
  344. //File block. Save it to tmp folder
  345. chunkFilepath := filepath.Join(uploadFolder, "upld_"+strconv.Itoa(blockCounter))
  346. chunkName = append(chunkName, chunkFilepath)
  347. ioutil.WriteFile(chunkFilepath, message, 0700)
  348. //Update the last upload chunk time
  349. lastChunkArrivalTime = time.Now().Unix()
  350. //Check if the file size is too big
  351. totalFileSize += fs.GetFileSize(chunkFilepath)
  352. if totalFileSize > max_upload_size {
  353. //File too big
  354. c.WriteMessage(1, []byte(`{\"error\":\"File size too large.\"}`))
  355. //Close the connection
  356. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  357. time.Sleep(1 * time.Second)
  358. c.Close()
  359. //Clear the tmp files
  360. os.RemoveAll(uploadFolder)
  361. return
  362. }
  363. blockCounter++
  364. //Request client to send the next chunk
  365. c.WriteMessage(1, []byte("next"))
  366. }
  367. //log.Println("recv:", len(message), "type", mt)
  368. }
  369. //Merge the file
  370. out, err := os.OpenFile(targetUploadLocation, os.O_CREATE|os.O_WRONLY, 0755)
  371. if err != nil {
  372. log.Println("Failed to open file:", err)
  373. c.WriteMessage(1, []byte(`{\"error\":\"Failed to open destination file\"}`))
  374. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  375. time.Sleep(1 * time.Second)
  376. c.Close()
  377. return
  378. }
  379. defer out.Close()
  380. for _, filesrc := range chunkName {
  381. srcChunkReader, err := os.Open(filesrc)
  382. if err != nil {
  383. log.Println("Failed to open Source Chunk", filesrc, " with error ", err.Error())
  384. c.WriteMessage(1, []byte(`{\"error\":\"Failed to open Source Chunk\"}`))
  385. return
  386. }
  387. io.Copy(out, srcChunkReader)
  388. srcChunkReader.Close()
  389. }
  390. //Set owner of the new uploaded file
  391. userinfo.SetOwnerOfFile(targetUploadLocation)
  392. //Return complete signal
  393. c.WriteMessage(1, []byte("OK"))
  394. //Stop the timeout listner
  395. done <- true
  396. //Clear the tmp folder
  397. time.Sleep(1 * time.Second)
  398. err = os.RemoveAll(uploadFolder)
  399. if err != nil {
  400. log.Println(err)
  401. }
  402. //Close WebSocket connection after finished
  403. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  404. time.Sleep(1 * time.Second)
  405. c.Close()
  406. }
  407. /*
  408. Handle FORM POST based upload
  409. This function is design for general SBCs or computers with more than 1GB of RAM
  410. (e.g. Raspberry Pi 4 / Linux Server)
  411. */
  412. func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
  413. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  414. if err != nil {
  415. sendErrorResponse(w, "User not logged in")
  416. return
  417. }
  418. username := userinfo.Username
  419. //Limit the max upload size to the user defined size
  420. if max_upload_size != 0 {
  421. r.Body = http.MaxBytesReader(w, r.Body, max_upload_size)
  422. }
  423. //Check if this is running under demo mode. If yes, reject upload
  424. if *demo_mode {
  425. sendErrorResponse(w, "You cannot upload in demo mode")
  426. return
  427. }
  428. err = r.ParseMultipartForm(int64(*upload_buf) << 20)
  429. if err != nil {
  430. //Filesize too big
  431. log.Println(err)
  432. sendErrorResponse(w, "File too large")
  433. return
  434. }
  435. file, handler, err := r.FormFile("file")
  436. if err != nil {
  437. log.Println("Error Retrieving File from upload by user: " + username)
  438. sendErrorResponse(w, "Unable to parse file from upload")
  439. return
  440. }
  441. //Get upload target directory
  442. uploadTarget, _ := mv(r, "path", true)
  443. if uploadTarget == "" {
  444. sendErrorResponse(w, "Upload target cannot be empty.")
  445. return
  446. }
  447. //Translate the upload target directory
  448. realUploadPath, err := userinfo.VirtualPathToRealPath(uploadTarget)
  449. if err != nil {
  450. sendErrorResponse(w, "Upload target is invalid or permission denied.")
  451. return
  452. }
  453. storeFilename := handler.Filename //Filename of the uploaded file
  454. destFilepath := filepath.ToSlash(filepath.Clean(realUploadPath)) + "/" + storeFilename
  455. if !fileExists(filepath.Dir(destFilepath)) {
  456. os.MkdirAll(filepath.Dir(destFilepath), 0755)
  457. }
  458. //Check if the upload target is read only.
  459. accmode := userinfo.GetPathAccessPermission(uploadTarget)
  460. if accmode == "readonly" {
  461. sendErrorResponse(w, "The upload target is Read Only.")
  462. return
  463. } else if accmode == "denied" {
  464. sendErrorResponse(w, "Access Denied")
  465. return
  466. }
  467. //Check for storage quota
  468. uploadFileSize := handler.Size
  469. if !userinfo.StorageQuota.HaveSpace(uploadFileSize) {
  470. sendErrorResponse(w, "Storage Quota Full")
  471. return
  472. }
  473. //Prepare the file to be created (uploaded)
  474. destination, err := os.Create(destFilepath)
  475. if err != nil {
  476. sendErrorResponse(w, err.Error())
  477. return
  478. }
  479. defer destination.Close()
  480. defer file.Close()
  481. //Move the file to destination file location
  482. if *enable_asyncFileUpload {
  483. //Use Async upload method
  484. go func(r *http.Request, file multipart.File, destination *os.File, userinfo *user.User) {
  485. //Do the file copying using a buffered reader
  486. buf := make([]byte, *file_opr_buff)
  487. for {
  488. n, err := file.Read(buf)
  489. if err != nil && err != io.EOF {
  490. log.Println(err.Error())
  491. return
  492. }
  493. if n == 0 {
  494. break
  495. }
  496. if _, err := destination.Write(buf[:n]); err != nil {
  497. log.Println(err.Error())
  498. return
  499. }
  500. }
  501. //Clear up buffered files
  502. r.MultipartForm.RemoveAll()
  503. //Set the ownership of file
  504. userinfo.SetOwnerOfFile(destFilepath)
  505. //Perform a GC afterward
  506. runtime.GC()
  507. }(r, file, destination, userinfo)
  508. } else {
  509. //Use blocking upload and move method
  510. buf := make([]byte, *file_opr_buff)
  511. for {
  512. n, err := file.Read(buf)
  513. if err != nil && err != io.EOF {
  514. log.Println(err.Error())
  515. return
  516. }
  517. if n == 0 {
  518. break
  519. }
  520. if _, err := destination.Write(buf[:n]); err != nil {
  521. log.Println(err.Error())
  522. return
  523. }
  524. }
  525. //Clear up buffered files
  526. r.MultipartForm.RemoveAll()
  527. //Set the ownership of file
  528. userinfo.SetOwnerOfFile(destFilepath)
  529. }
  530. //Finish up the upload
  531. //fmt.Printf("Uploaded File: %+v\n", handler.Filename)
  532. //fmt.Printf("File Size: %+v\n", handler.Size)
  533. //fmt.Printf("MIME Header: %+v\n", handler.Header)
  534. //fmt.Println("Upload target: " + realUploadPath)
  535. //Fnish upload. Fix the tmp filename
  536. log.Println(username + " uploaded a file: " + handler.Filename)
  537. //Do upload finishing stuff
  538. //Perform a GC
  539. runtime.GC()
  540. //Completed
  541. sendOK(w)
  542. return
  543. }
  544. //Validate if the copy and target process will involve file overwriting problem.
  545. func system_fs_validateFileOpr(w http.ResponseWriter, r *http.Request) {
  546. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  547. if err != nil {
  548. sendErrorResponse(w, err.Error())
  549. return
  550. }
  551. vsrcFiles, _ := mv(r, "src", true)
  552. vdestFile, _ := mv(r, "dest", true)
  553. var duplicateFiles []string
  554. //Loop through all files are see if there are duplication during copy and paste
  555. sourceFiles := []string{}
  556. decodedSourceFiles, _ := url.QueryUnescape(vsrcFiles)
  557. err = json.Unmarshal([]byte(decodedSourceFiles), &sourceFiles)
  558. if err != nil {
  559. sendErrorResponse(w, "Source file JSON parse error.")
  560. return
  561. }
  562. rdestFile, _ := userinfo.VirtualPathToRealPath(vdestFile)
  563. for _, file := range sourceFiles {
  564. rsrcFile, _ := userinfo.VirtualPathToRealPath(string(file))
  565. if fileExists(rdestFile + filepath.Base(rsrcFile)) {
  566. //File exists already.
  567. vpath, _ := userinfo.RealPathToVirtualPath(rsrcFile)
  568. duplicateFiles = append(duplicateFiles, vpath)
  569. }
  570. }
  571. jsonString, _ := json.Marshal(duplicateFiles)
  572. sendJSONResponse(w, string(jsonString))
  573. return
  574. }
  575. //Scan all directory and get trash file and send back results with WebSocket
  576. func system_fs_WebSocketScanTrashBin(w http.ResponseWriter, r *http.Request) {
  577. //Get and check user permission
  578. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  579. if err != nil {
  580. sendErrorResponse(w, "User not logged in")
  581. return
  582. }
  583. //Upgrade to websocket
  584. var upgrader = websocket.Upgrader{}
  585. c, err := upgrader.Upgrade(w, r, nil)
  586. if err != nil {
  587. w.WriteHeader(http.StatusInternalServerError)
  588. w.Write([]byte("500 - " + err.Error()))
  589. log.Print("Websocket Upgrade Error:", err.Error())
  590. return
  591. }
  592. //Start Scanning
  593. scanningRoots := []string{}
  594. //Get all roots to scan
  595. for _, storage := range userinfo.GetAllFileSystemHandler() {
  596. storageRoot := storage.Path
  597. scanningRoots = append(scanningRoots, storageRoot)
  598. }
  599. for _, rootPath := range scanningRoots {
  600. err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
  601. oneLevelUpper := filepath.Base(filepath.Dir(path))
  602. if oneLevelUpper == ".trash" {
  603. //This is a trashbin dir.
  604. file := path
  605. //Parse the trashFile struct
  606. timestamp := filepath.Ext(file)[1:]
  607. originalName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file)))
  608. originalExt := filepath.Ext(filepath.Base(originalName))
  609. virtualFilepath, _ := userinfo.RealPathToVirtualPath(file)
  610. virtualOrgPath, _ := userinfo.RealPathToVirtualPath(filepath.Dir(filepath.Dir(file)))
  611. rawsize := fs.GetFileSize(file)
  612. timestampInt64, _ := StringToInt64(timestamp)
  613. removeTimeDate := time.Unix(timestampInt64, 0)
  614. if IsDir(file) {
  615. originalExt = ""
  616. }
  617. thisTrashFileObject := trashedFile{
  618. Filename: filepath.Base(file),
  619. Filepath: virtualFilepath,
  620. FileExt: originalExt,
  621. IsDir: IsDir(file),
  622. Filesize: int64(rawsize),
  623. RemoveTimestamp: timestampInt64,
  624. RemoveDate: timeToString(removeTimeDate),
  625. OriginalPath: virtualOrgPath,
  626. OriginalFilename: originalName,
  627. }
  628. //Send out the result as JSON string
  629. js, _ := json.Marshal(thisTrashFileObject)
  630. err := c.WriteMessage(1, js)
  631. if err != nil {
  632. //Connection already closed
  633. return err
  634. }
  635. }
  636. return nil
  637. })
  638. if err != nil {
  639. //Scan or client connection error (Connection closed?)
  640. return
  641. }
  642. }
  643. //Close connection after finished
  644. c.Close()
  645. }
  646. //Scan all the directory and get trash files within the system
  647. func system_fs_scanTrashBin(w http.ResponseWriter, r *http.Request) {
  648. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  649. if err != nil {
  650. sendErrorResponse(w, err.Error())
  651. return
  652. }
  653. username := userinfo.Username
  654. results := []trashedFile{}
  655. files, err := system_fs_listTrash(username)
  656. if err != nil {
  657. sendErrorResponse(w, err.Error())
  658. return
  659. }
  660. //Get information of each files and process it into results
  661. for _, file := range files {
  662. timestamp := filepath.Ext(file)[1:]
  663. originalName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file)))
  664. originalExt := filepath.Ext(filepath.Base(originalName))
  665. virtualFilepath, _ := userinfo.RealPathToVirtualPath(file)
  666. virtualOrgPath, _ := userinfo.RealPathToVirtualPath(filepath.Dir(filepath.Dir(file)))
  667. rawsize := fs.GetFileSize(file)
  668. timestampInt64, _ := StringToInt64(timestamp)
  669. removeTimeDate := time.Unix(timestampInt64, 0)
  670. if IsDir(file) {
  671. originalExt = ""
  672. }
  673. results = append(results, trashedFile{
  674. Filename: filepath.Base(file),
  675. Filepath: virtualFilepath,
  676. FileExt: originalExt,
  677. IsDir: IsDir(file),
  678. Filesize: int64(rawsize),
  679. RemoveTimestamp: timestampInt64,
  680. RemoveDate: timeToString(removeTimeDate),
  681. OriginalPath: virtualOrgPath,
  682. OriginalFilename: originalName,
  683. })
  684. }
  685. //Sort the results by date, latest on top
  686. sort.Slice(results[:], func(i, j int) bool {
  687. return results[i].RemoveTimestamp > results[j].RemoveTimestamp
  688. })
  689. //Format and return the json results
  690. jsonString, _ := json.Marshal(results)
  691. sendJSONResponse(w, string(jsonString))
  692. }
  693. //Restore a trashed file to its parent dir
  694. func system_fs_restoreFile(w http.ResponseWriter, r *http.Request) {
  695. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  696. if err != nil {
  697. sendErrorResponse(w, err.Error())
  698. return
  699. }
  700. targetTrashedFile, err := mv(r, "src", true)
  701. if err != nil {
  702. sendErrorResponse(w, "Invalid src given")
  703. return
  704. }
  705. //Translate it to realpath
  706. realpath, _ := userinfo.VirtualPathToRealPath(targetTrashedFile)
  707. if !fileExists(realpath) {
  708. sendErrorResponse(w, "File not exists")
  709. return
  710. }
  711. //Check if this is really a trashed file
  712. if filepath.Base(filepath.Dir(realpath)) != ".trash" {
  713. sendErrorResponse(w, "File not in trashbin")
  714. return
  715. }
  716. //OK to proceed.
  717. targetPath := filepath.ToSlash(filepath.Dir(filepath.Dir(realpath))) + "/" + strings.TrimSuffix(filepath.Base(realpath), filepath.Ext(filepath.Base(realpath)))
  718. //log.Println(targetPath);
  719. os.Rename(realpath, targetPath)
  720. //Check if the parent dir has no more fileds. If yes, remove it
  721. filescounter, _ := filepath.Glob(filepath.Dir(realpath) + "/*")
  722. if len(filescounter) == 0 {
  723. os.Remove(filepath.Dir(realpath))
  724. }
  725. sendOK(w)
  726. }
  727. //Clear all trashed file in the system
  728. func system_fs_clearTrashBin(w http.ResponseWriter, r *http.Request) {
  729. u, err := userHandler.GetUserInfoFromRequest(w, r)
  730. if err != nil {
  731. sendErrorResponse(w, "User not logged in")
  732. return
  733. }
  734. username := u.Username
  735. fileList, err := system_fs_listTrash(username)
  736. if err != nil {
  737. sendErrorResponse(w, "Unable to clear trash: "+err.Error())
  738. return
  739. }
  740. //Get list success. Remove each of them.
  741. for _, file := range fileList {
  742. isOwner := u.IsOwnerOfFile(file)
  743. if isOwner {
  744. //This user own this system. Remove this file from his quota
  745. u.RemoveOwnershipFromFile(file)
  746. }
  747. os.RemoveAll(file)
  748. //Check if its parent directory have no files. If yes, remove the dir itself as well.
  749. filesInThisTrashBin, _ := filepath.Glob(filepath.Dir(file) + "/*")
  750. if len(filesInThisTrashBin) == 0 {
  751. os.Remove(filepath.Dir(file))
  752. }
  753. }
  754. sendOK(w)
  755. }
  756. //Get all trash in a string list
  757. func system_fs_listTrash(username string) ([]string, error) {
  758. userinfo, _ := userHandler.GetUserInfoFromUsername(username)
  759. scanningRoots := []string{}
  760. //Get all roots to scan
  761. for _, storage := range userinfo.GetAllFileSystemHandler() {
  762. storageRoot := storage.Path
  763. scanningRoots = append(scanningRoots, storageRoot)
  764. }
  765. files := []string{}
  766. for _, rootPath := range scanningRoots {
  767. err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
  768. oneLevelUpper := filepath.Base(filepath.Dir(path))
  769. if oneLevelUpper == ".trash" {
  770. //This is a trashbin dir.
  771. files = append(files, path)
  772. }
  773. return nil
  774. })
  775. if err != nil {
  776. return []string{}, errors.New("Failed to scan file system.")
  777. }
  778. }
  779. return files, nil
  780. }
  781. /*
  782. Handle new file or folder functions
  783. Required information
  784. @type {folder / file}
  785. @ext {any that is listed in the template folder}
  786. if no paramter is passed in, default listing all the supported template file
  787. */
  788. func system_fs_handleNewObjects(w http.ResponseWriter, r *http.Request) {
  789. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  790. if err != nil {
  791. sendErrorResponse(w, "User not logged in")
  792. return
  793. }
  794. fileType, _ := mv(r, "type", true) //File creation type, {file, folder}
  795. vsrc, _ := mv(r, "src", true) //Virtual file source folder, do not include filename
  796. filename, _ := mv(r, "filename", true) //Filename for the new file
  797. if fileType == "" && filename == "" {
  798. //List all the supported new filetype
  799. if !fileExists("system/newitem/") {
  800. os.MkdirAll("system/newitem/", 0755)
  801. }
  802. type newItemObject struct {
  803. Desc string
  804. Ext string
  805. }
  806. var newItemList []newItemObject
  807. newItemTemplate, _ := filepath.Glob("system/newitem/*")
  808. for _, file := range newItemTemplate {
  809. thisItem := new(newItemObject)
  810. thisItem.Desc = strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
  811. thisItem.Ext = filepath.Ext(file)[1:]
  812. newItemList = append(newItemList, *thisItem)
  813. }
  814. jsonString, err := json.Marshal(newItemList)
  815. if err != nil {
  816. log.Println("*File System* Unable to parse JSON string for new item list!")
  817. sendErrorResponse(w, "Unable to parse new item list. See server log for more information.")
  818. return
  819. }
  820. sendJSONResponse(w, string(jsonString))
  821. return
  822. } else if fileType != "" && filename != "" {
  823. if vsrc == "" {
  824. sendErrorResponse(w, "Missing paramter: 'src'")
  825. return
  826. }
  827. //Translate the path to realpath
  828. rpath, err := userinfo.VirtualPathToRealPath(vsrc)
  829. if err != nil {
  830. sendErrorResponse(w, "Invalid path given.")
  831. return
  832. }
  833. //Check if directory is readonly
  834. accmode := userinfo.GetPathAccessPermission(vsrc)
  835. if accmode == "readonly" {
  836. sendErrorResponse(w, "This directory is Read Only.")
  837. return
  838. } else if accmode == "denied" {
  839. sendErrorResponse(w, "Access Denied")
  840. return
  841. }
  842. //Check if the file already exists. If yes, fix its filename.
  843. newfilePath := rpath + filename
  844. if fileType == "file" {
  845. for fileExists(newfilePath) {
  846. sendErrorResponse(w, "Given filename already exists.")
  847. return
  848. }
  849. ext := filepath.Ext(filename)
  850. if ext == "" {
  851. //This is a file with no extension.
  852. f, err := os.Create(newfilePath)
  853. if err != nil {
  854. log.Println("*File System* " + err.Error())
  855. sendErrorResponse(w, err.Error())
  856. return
  857. }
  858. f.Close()
  859. } else {
  860. templateFile, _ := filepath.Glob("system/newitem/*" + ext)
  861. if len(templateFile) == 0 {
  862. //This file extension is not in template
  863. f, err := os.Create(newfilePath)
  864. if err != nil {
  865. log.Println("*File System* " + err.Error())
  866. sendErrorResponse(w, err.Error())
  867. return
  868. }
  869. f.Close()
  870. } else {
  871. //Copy file from templateFile[0] to current dir with the given name
  872. input, _ := ioutil.ReadFile(templateFile[0])
  873. err := ioutil.WriteFile(newfilePath, input, 0755)
  874. if err != nil {
  875. log.Println("*File System* " + err.Error())
  876. sendErrorResponse(w, err.Error())
  877. return
  878. }
  879. }
  880. }
  881. } else if fileType == "folder" {
  882. if fileExists(newfilePath) {
  883. sendErrorResponse(w, "Given folder already exists.")
  884. return
  885. }
  886. //Create the folder at target location
  887. err := os.Mkdir(newfilePath, 0755)
  888. if err != nil {
  889. sendErrorResponse(w, err.Error())
  890. return
  891. }
  892. }
  893. sendJSONResponse(w, "\"OK\"")
  894. } else {
  895. sendErrorResponse(w, "Missing paramter(s).")
  896. return
  897. }
  898. }
  899. /*
  900. Handle file operations via WebSocket
  901. This handler only handle zip, unzip, copy and move. Not other operations.
  902. For other operations, please use the legacy handleOpr endpoint
  903. */
  904. func system_fs_handleWebSocketOpr(w http.ResponseWriter, r *http.Request) {
  905. //Get and check user permission
  906. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  907. if err != nil {
  908. sendErrorResponse(w, "User not logged in")
  909. return
  910. }
  911. operation, _ := mv(r, "opr", false) //Accept copy and move
  912. vsrcFiles, _ := mv(r, "src", false)
  913. vdestFile, _ := mv(r, "dest", false)
  914. existsOpr, _ := mv(r, "existsresp", false)
  915. if existsOpr == "" {
  916. existsOpr = "keep"
  917. }
  918. //Decode the source file list
  919. var sourceFiles []string
  920. tmp := []string{}
  921. decodedSourceFiles, _ := url.QueryUnescape(vsrcFiles)
  922. err = json.Unmarshal([]byte(decodedSourceFiles), &sourceFiles)
  923. if err != nil {
  924. log.Println("Source file JSON parse error.", err.Error())
  925. sendErrorResponse(w, "Source file JSON parse error.")
  926. return
  927. }
  928. //Bugged char filtering
  929. for _, src := range sourceFiles {
  930. tmp = append(tmp, strings.ReplaceAll(src, "{{plug_sign}}", "+"))
  931. }
  932. sourceFiles = tmp
  933. vdestFile = strings.ReplaceAll(vdestFile, "{{plug_sign}}", "+")
  934. //Decode the target position
  935. escapedVdest, _ := url.QueryUnescape(vdestFile)
  936. vdestFile = escapedVdest
  937. rdestFile, _ := userinfo.VirtualPathToRealPath(vdestFile)
  938. //Permission checking
  939. if !userinfo.CanWrite(vdestFile) {
  940. log.Println(vdestFile)
  941. w.WriteHeader(http.StatusForbidden)
  942. w.Write([]byte("403 - Access Denied"))
  943. return
  944. }
  945. //Check if opr is suported
  946. if operation == "move" || operation == "copy" || operation == "zip" || operation == "unzip" {
  947. } else {
  948. log.Println("This file operation is not supported on WebSocket file operations endpoint. Please use the legacy endpoint instead. Received: ", operation)
  949. w.WriteHeader(http.StatusInternalServerError)
  950. w.Write([]byte("500 - Not supported operation"))
  951. return
  952. }
  953. //Upgrade to websocket
  954. var upgrader = websocket.Upgrader{}
  955. c, err := upgrader.Upgrade(w, r, nil)
  956. if err != nil {
  957. w.WriteHeader(http.StatusInternalServerError)
  958. w.Write([]byte("500 - " + err.Error()))
  959. log.Print("Websocket Upgrade Error:", err.Error())
  960. return
  961. }
  962. type ProgressUpdate struct {
  963. LatestFile string
  964. Progress int
  965. Error string
  966. }
  967. if operation == "zip" {
  968. //Zip files
  969. outputFilename := filepath.Join(rdestFile, filepath.Base(rdestFile)) + ".zip"
  970. if len(sourceFiles) == 1 {
  971. //Use the basename of the source file as zip file name
  972. outputFilename = filepath.Join(rdestFile, filepath.Base(sourceFiles[0])) + ".zip"
  973. }
  974. //Translate source Files into real paths
  975. realSourceFiles := []string{}
  976. for _, vsrcs := range sourceFiles {
  977. rsrc, err := userinfo.VirtualPathToRealPath(vsrcs)
  978. if err != nil {
  979. stopStatus := ProgressUpdate{
  980. LatestFile: filepath.Base(rsrc),
  981. Progress: -1,
  982. Error: "File not exists",
  983. }
  984. js, _ := json.Marshal(stopStatus)
  985. c.WriteMessage(1, js)
  986. c.Close()
  987. }
  988. realSourceFiles = append(realSourceFiles, rsrc)
  989. }
  990. //Create the zip file
  991. fs.ArozZipFileWithProgress(realSourceFiles, outputFilename, false, func(currentFilename string, _ int, _ int, progress float64) {
  992. currentStatus := ProgressUpdate{
  993. LatestFile: currentFilename,
  994. Progress: int(math.Ceil(progress)),
  995. Error: "",
  996. }
  997. js, _ := json.Marshal(currentStatus)
  998. c.WriteMessage(1, js)
  999. })
  1000. } else if operation == "unzip" {
  1001. //Check if the target destination exists and writable
  1002. if !userinfo.CanWrite(vdestFile) {
  1003. stopStatus := ProgressUpdate{
  1004. LatestFile: filepath.Base(vdestFile),
  1005. Progress: -1,
  1006. Error: "Access Denied: No Write Permission",
  1007. }
  1008. js, _ := json.Marshal(stopStatus)
  1009. c.WriteMessage(1, js)
  1010. c.Close()
  1011. }
  1012. //Create the destination folder
  1013. os.MkdirAll(rdestFile, 0755)
  1014. //Convert the src files into realpaths
  1015. realSourceFiles := []string{}
  1016. for _, vsrcs := range sourceFiles {
  1017. rsrc, err := userinfo.VirtualPathToRealPath(vsrcs)
  1018. if err != nil {
  1019. stopStatus := ProgressUpdate{
  1020. LatestFile: filepath.Base(rsrc),
  1021. Progress: -1,
  1022. Error: "File not exists",
  1023. }
  1024. js, _ := json.Marshal(stopStatus)
  1025. c.WriteMessage(1, js)
  1026. c.Close()
  1027. }
  1028. realSourceFiles = append(realSourceFiles, rsrc)
  1029. }
  1030. //Unzip the files
  1031. fs.ArozUnzipFileWithProgress(realSourceFiles, rdestFile, func(currentFile string, filecount int, totalfile int, progress float64) {
  1032. //Generate the status update struct
  1033. currentStatus := ProgressUpdate{
  1034. LatestFile: filepath.Base(currentFile),
  1035. Progress: int(math.Ceil(progress)),
  1036. Error: "",
  1037. }
  1038. js, _ := json.Marshal(currentStatus)
  1039. c.WriteMessage(1, js)
  1040. })
  1041. } else {
  1042. //Other operations that allow multiple source files to handle one by one
  1043. for i := 0; i < len(sourceFiles); i++ {
  1044. vsrcFile := sourceFiles[i]
  1045. rsrcFile, _ := userinfo.VirtualPathToRealPath(vsrcFile)
  1046. //c.WriteMessage(1, message)
  1047. if !fileExists(rsrcFile) {
  1048. //This source file not exists. Report Error and Stop
  1049. stopStatus := ProgressUpdate{
  1050. LatestFile: filepath.Base(rsrcFile),
  1051. Progress: -1,
  1052. Error: "File not exists",
  1053. }
  1054. js, _ := json.Marshal(stopStatus)
  1055. c.WriteMessage(1, js)
  1056. c.Close()
  1057. return
  1058. }
  1059. if operation == "move" {
  1060. fs.FileMove(rsrcFile, rdestFile, existsOpr, false, func(progress int, currentFile string) {
  1061. //Multply child progress to parent progress
  1062. blockRatio := float64(100) / float64(len(sourceFiles))
  1063. overallRatio := blockRatio*float64(i) + blockRatio*(float64(progress)/float64(100))
  1064. //Construct return struct
  1065. currentStatus := ProgressUpdate{
  1066. LatestFile: filepath.Base(currentFile),
  1067. Progress: int(overallRatio),
  1068. Error: "",
  1069. }
  1070. js, _ := json.Marshal(currentStatus)
  1071. c.WriteMessage(1, js)
  1072. })
  1073. } else if operation == "copy" {
  1074. fs.FileCopy(rsrcFile, rdestFile, existsOpr, func(progress int, currentFile string) {
  1075. //Multply child progress to parent progress
  1076. blockRatio := float64(100) / float64(len(sourceFiles))
  1077. overallRatio := blockRatio*float64(i) + blockRatio*(float64(progress)/float64(100))
  1078. //Construct return struct
  1079. currentStatus := ProgressUpdate{
  1080. LatestFile: filepath.Base(currentFile),
  1081. Progress: int(overallRatio),
  1082. Error: "",
  1083. }
  1084. js, _ := json.Marshal(currentStatus)
  1085. c.WriteMessage(1, js)
  1086. })
  1087. }
  1088. }
  1089. }
  1090. //Close WebSocket connection after finished
  1091. time.Sleep(1 * time.Second)
  1092. c.WriteControl(8, []byte{}, time.Now().Add(time.Second))
  1093. c.Close()
  1094. }
  1095. /*
  1096. Handle file operations
  1097. Support {move, copy, delete, recycle, rename}
  1098. */
  1099. //Handle file operations.
  1100. func system_fs_handleOpr(w http.ResponseWriter, r *http.Request) {
  1101. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1102. if err != nil {
  1103. sendErrorResponse(w, "User not logged in")
  1104. return
  1105. }
  1106. operation, _ := mv(r, "opr", true)
  1107. vsrcFiles, _ := mv(r, "src", true)
  1108. vdestFile, _ := mv(r, "dest", true)
  1109. vnfilenames, _ := mv(r, "new", true) //Only use when rename or create new file / folder
  1110. //Check if operation valid.
  1111. if operation == "" {
  1112. //Undefined operations.
  1113. sendErrorResponse(w, "Undefined operations paramter: Missing 'opr' in request header.")
  1114. return
  1115. }
  1116. //As the user can pass in multiple source files at the same time, parse sourceFiles from json string
  1117. var sourceFiles []string
  1118. //This line is required in order to allow passing of special charaters
  1119. decodedSourceFiles := system_fs_specialURIDecode(vsrcFiles)
  1120. err = json.Unmarshal([]byte(decodedSourceFiles), &sourceFiles)
  1121. if err != nil {
  1122. sendErrorResponse(w, "Source file JSON parse error.")
  1123. return
  1124. }
  1125. //Check if new filenames are also valid. If yes, translate it into string array
  1126. var newFilenames []string
  1127. if vnfilenames != "" {
  1128. vnfilenames, _ := url.QueryUnescape(vnfilenames)
  1129. err = json.Unmarshal([]byte(vnfilenames), &newFilenames)
  1130. if err != nil {
  1131. sendErrorResponse(w, "Unable to parse JSON for new filenames")
  1132. return
  1133. }
  1134. }
  1135. if operation == "zip" {
  1136. //Zip operation. Parse the real filepath list
  1137. rsrcFiles := []string{}
  1138. rdestFile, _ := userinfo.VirtualPathToRealPath(vdestFile)
  1139. for _, vsrcFile := range sourceFiles {
  1140. rsrcFile, _ := userinfo.VirtualPathToRealPath(string(vsrcFile))
  1141. if fileExists(rsrcFile) {
  1142. rsrcFiles = append(rsrcFiles, rsrcFile)
  1143. }
  1144. }
  1145. zipFilename := rdestFile
  1146. if fs.IsDir(rdestFile) {
  1147. //Append the filename to it
  1148. if len(rsrcFiles) == 1 {
  1149. zipFilename = filepath.Join(rdestFile, strings.TrimSuffix(filepath.Base(rsrcFiles[0]), filepath.Ext(filepath.Base(rsrcFiles[0])))+".zip")
  1150. } else if len(rsrcFiles) > 1 {
  1151. zipFilename = filepath.Join(rdestFile, filepath.Base(filepath.Dir(rsrcFiles[0]))+".zip")
  1152. }
  1153. }
  1154. //Create a zip file at target location
  1155. err := fs.ArozZipFile(rsrcFiles, zipFilename, false)
  1156. if err != nil {
  1157. sendErrorResponse(w, err.Error())
  1158. return
  1159. }
  1160. } else {
  1161. //For operations that is handled file by file
  1162. for i, vsrcFile := range sourceFiles {
  1163. //Convert the virtual path to realpath on disk
  1164. rsrcFile, _ := userinfo.VirtualPathToRealPath(string(vsrcFile))
  1165. rdestFile, _ := userinfo.VirtualPathToRealPath(vdestFile)
  1166. //Check if the source file exists
  1167. if !fileExists(rsrcFile) {
  1168. /*
  1169. Special edge case handler:
  1170. There might be edge case that files are stored in URIEncoded methods
  1171. e.g. abc def.mp3 --> abc%20cdf.mp3
  1172. In this case, this logic statement should be able to handle this
  1173. */
  1174. edgeCaseFilename := filepath.Join(filepath.Dir(rsrcFile), system_fs_specialURIEncode(filepath.Base(rsrcFile)))
  1175. if fileExists(edgeCaseFilename) {
  1176. rsrcFile = edgeCaseFilename
  1177. } else {
  1178. sendErrorResponse(w, "Source file not exists.")
  1179. return
  1180. }
  1181. }
  1182. if operation == "rename" {
  1183. //Check if the usage is correct.
  1184. if vdestFile != "" {
  1185. sendErrorResponse(w, "Rename only accept 'src' and 'new'. Please use move if you want to move a file.")
  1186. return
  1187. }
  1188. //Check if new name paramter is passed in.
  1189. if len(newFilenames) == 0 {
  1190. sendErrorResponse(w, "Missing paramter (JSON string): 'new'")
  1191. return
  1192. }
  1193. //Check if the source filenames and new filenanmes match
  1194. if len(newFilenames) != len(sourceFiles) {
  1195. sendErrorResponse(w, "New filenames do not match with source filename's length.")
  1196. return
  1197. }
  1198. //Check if the target dir is not readonly
  1199. accmode := userinfo.GetPathAccessPermission(string(vsrcFile))
  1200. if accmode == "readonly" {
  1201. sendErrorResponse(w, "This directory is Read Only.")
  1202. return
  1203. } else if accmode == "denied" {
  1204. sendErrorResponse(w, "Access Denied")
  1205. return
  1206. }
  1207. thisFilename := newFilenames[i]
  1208. //Check if the name already exists. If yes, return false
  1209. if fileExists(filepath.Dir(rsrcFile) + "/" + thisFilename) {
  1210. sendErrorResponse(w, "File already exists")
  1211. return
  1212. }
  1213. //Everything is ok. Rename the file.
  1214. targetNewName := filepath.Dir(rsrcFile) + "/" + thisFilename
  1215. err = os.Rename(rsrcFile, targetNewName)
  1216. if err != nil {
  1217. sendErrorResponse(w, err.Error())
  1218. return
  1219. }
  1220. } else if operation == "move" {
  1221. //File move operation. Check if the source file / dir and target directory exists
  1222. /*
  1223. Example usage from file explorer
  1224. $.ajax({
  1225. type: 'POST',
  1226. url: `/system/file_system/fileOpr`,
  1227. data: {opr: "move" ,src: JSON.stringify(fileList), dest: targetDir},
  1228. success: function(data){
  1229. if (data.error !== undefined){
  1230. msgbox("remove",data.error);
  1231. }else{
  1232. //OK, do something
  1233. }
  1234. }
  1235. });
  1236. */
  1237. if !fileExists(rsrcFile) {
  1238. sendErrorResponse(w, "Source file not exists")
  1239. return
  1240. }
  1241. //Check if the source file is read only.
  1242. accmode := userinfo.GetPathAccessPermission(string(vsrcFile))
  1243. if accmode == "readonly" {
  1244. sendErrorResponse(w, "This source file is Read Only.")
  1245. return
  1246. } else if accmode == "denied" {
  1247. sendErrorResponse(w, "Access Denied")
  1248. return
  1249. }
  1250. if rdestFile == "" {
  1251. sendErrorResponse(w, "Undefined dest location.")
  1252. return
  1253. }
  1254. //Get exists overwrite mode
  1255. existsOpr, _ := mv(r, "existsresp", true)
  1256. //Check if use fast move instead
  1257. //Check if the source and destination folder are under the same root. If yes, use os.Rename for faster move operations
  1258. underSameRoot := false
  1259. //Check if the two files are under the same user root path
  1260. srcAbs, _ := filepath.Abs(rsrcFile)
  1261. destAbs, _ := filepath.Abs(rdestFile)
  1262. //Check other storage path and see if they are under the same root
  1263. for _, rootPath := range userinfo.GetAllFileSystemHandler() {
  1264. thisRoot := rootPath.Path
  1265. thisRootAbs, err := filepath.Abs(thisRoot)
  1266. if err != nil {
  1267. continue
  1268. }
  1269. if strings.Contains(srcAbs, thisRootAbs) && strings.Contains(destAbs, thisRootAbs) {
  1270. underSameRoot = true
  1271. }
  1272. }
  1273. //Updates 19-10-2020: Added ownership management to file move and copy
  1274. userinfo.RemoveOwnershipFromFile(rsrcFile)
  1275. err = fs.FileMove(rsrcFile, rdestFile, existsOpr, underSameRoot, nil)
  1276. if err != nil {
  1277. sendErrorResponse(w, err.Error())
  1278. //Restore the ownership if remove failed
  1279. userinfo.SetOwnerOfFile(rsrcFile)
  1280. return
  1281. }
  1282. //Set user to own the new file
  1283. userinfo.SetOwnerOfFile(filepath.ToSlash(filepath.Clean(rdestFile)) + "/" + filepath.Base(rsrcFile))
  1284. } else if operation == "copy" {
  1285. //Copy file. See move example and change 'opr' to 'copy'
  1286. if !fileExists(rsrcFile) {
  1287. sendErrorResponse(w, "Source file not exists")
  1288. return
  1289. }
  1290. //Check if the desintation is read only.
  1291. if !userinfo.CanWrite(vdestFile) {
  1292. sendErrorResponse(w, "Access Denied.")
  1293. return
  1294. }
  1295. if !fileExists(rdestFile) {
  1296. if fileExists(filepath.Dir(rdestFile)) {
  1297. //User pass in the whole path for the folder. Report error usecase.
  1298. sendErrorResponse(w, "Dest location should be an existing folder instead of the full path of the copied file.")
  1299. return
  1300. }
  1301. sendErrorResponse(w, "Dest folder not found")
  1302. return
  1303. }
  1304. existsOpr, _ := mv(r, "existsresp", true)
  1305. //Check if the user have space for the extra file
  1306. if !userinfo.StorageQuota.HaveSpace(fs.GetFileSize(rdestFile)) {
  1307. sendErrorResponse(w, "Storage Quota Full")
  1308. return
  1309. }
  1310. err = fs.FileCopy(rsrcFile, rdestFile, existsOpr, nil)
  1311. if err != nil {
  1312. sendErrorResponse(w, err.Error())
  1313. return
  1314. }
  1315. //Set user to own this file
  1316. userinfo.SetOwnerOfFile(filepath.ToSlash(filepath.Clean(rdestFile)) + "/" + filepath.Base(rsrcFile))
  1317. } else if operation == "delete" {
  1318. //Delete the file permanently
  1319. if !fileExists(rsrcFile) {
  1320. //Check if it is a non escapted file instead
  1321. sendErrorResponse(w, "Source file not exists")
  1322. return
  1323. }
  1324. //Check if the desintation is read only.
  1325. /*
  1326. accmode := userinfo.GetPathAccessPermission(string(vsrcFile))
  1327. if accmode == "readonly" {
  1328. sendErrorResponse(w, "This directory is Read Only.")
  1329. return
  1330. } else if accmode == "denied" {
  1331. sendErrorResponse(w, "Access Denied")
  1332. return
  1333. }
  1334. */
  1335. if !userinfo.CanWrite(vsrcFile) {
  1336. sendErrorResponse(w, "Access Denied.")
  1337. return
  1338. }
  1339. //Check if the user own this file
  1340. isOwner := userinfo.IsOwnerOfFile(rsrcFile)
  1341. if isOwner {
  1342. //This user own this system. Remove this file from his quota
  1343. userinfo.RemoveOwnershipFromFile(rsrcFile)
  1344. }
  1345. //Check if this file has any cached files. If yes, remove it
  1346. if fileExists(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/" + filepath.Base(rsrcFile) + ".jpg") {
  1347. os.Remove(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/" + filepath.Base(rsrcFile) + ".jpg")
  1348. }
  1349. //Clear the cache folder if there is no files inside
  1350. fc, _ := filepath.Glob(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/*")
  1351. if len(fc) == 0 {
  1352. os.Remove(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/")
  1353. }
  1354. os.RemoveAll(rsrcFile)
  1355. } else if operation == "recycle" {
  1356. //Put it into a subfolder named trash and allow it to to be removed later
  1357. if !fileExists(rsrcFile) {
  1358. //Check if it is a non escapted file instead
  1359. sendErrorResponse(w, "Source file not exists")
  1360. return
  1361. }
  1362. //Check if the upload target is read only.
  1363. //Updates 20 Jan 2021: Replace with CanWrite handler
  1364. /*
  1365. accmode := userinfo.GetPathAccessPermission(string(vsrcFile))
  1366. if accmode == "readonly" {
  1367. sendErrorResponse(w, "This directory is Read Only.")
  1368. return
  1369. } else if accmode == "denied" {
  1370. sendErrorResponse(w, "Access Denied")
  1371. return
  1372. }*/
  1373. if !userinfo.CanWrite(vsrcFile) {
  1374. sendErrorResponse(w, "Access Denied.")
  1375. return
  1376. }
  1377. //Check if this file has any cached files. If yes, remove it
  1378. if fileExists(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/" + filepath.Base(rsrcFile) + ".jpg") {
  1379. os.Remove(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/" + filepath.Base(rsrcFile) + ".jpg")
  1380. }
  1381. //Clear the cache folder if there is no files inside
  1382. fc, _ := filepath.Glob(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/*")
  1383. if len(fc) == 0 {
  1384. os.Remove(filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.cache/")
  1385. }
  1386. //Create a trash directory for this folder
  1387. trashDir := filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.trash/"
  1388. os.MkdirAll(trashDir, 0755)
  1389. hidden.HideFile(trashDir)
  1390. os.Rename(rsrcFile, trashDir+filepath.Base(rsrcFile)+"."+Int64ToString(GetUnixTime()))
  1391. } else if operation == "unzip" {
  1392. //Unzip the file to destination
  1393. //Check if the user can write to the target dest file
  1394. if userinfo.CanWrite(string(vdestFile)) == false {
  1395. sendErrorResponse(w, "Access Denied.")
  1396. return
  1397. }
  1398. //Make the rdest directory if not exists
  1399. if !fileExists(rdestFile) {
  1400. err = os.MkdirAll(rdestFile, 0755)
  1401. if err != nil {
  1402. sendErrorResponse(w, err.Error())
  1403. return
  1404. }
  1405. }
  1406. //OK! Unzip to destination
  1407. err := fs.Unzip(rsrcFile, rdestFile)
  1408. if err != nil {
  1409. sendErrorResponse(w, err.Error())
  1410. return
  1411. }
  1412. } else {
  1413. sendErrorResponse(w, "Unknown file opeartion given.")
  1414. return
  1415. }
  1416. }
  1417. }
  1418. sendJSONResponse(w, "\"OK\"")
  1419. return
  1420. }
  1421. //Allow systems to store key value pairs in the database as preferences.
  1422. func system_fs_handleUserPreference(w http.ResponseWriter, r *http.Request) {
  1423. username, err := authAgent.GetUserName(w, r)
  1424. if err != nil {
  1425. sendErrorResponse(w, "User not logged in")
  1426. return
  1427. }
  1428. key, _ := mv(r, "key", false)
  1429. value, _ := mv(r, "value", false)
  1430. if key != "" && value == "" {
  1431. //Get mode. Read the prefernece with given key
  1432. result := ""
  1433. err := sysdb.Read("fs", "pref/"+key+"/"+username, &result)
  1434. if err != nil {
  1435. sendJSONResponse(w, "{\"error\":\"Key not found.\"}")
  1436. return
  1437. }
  1438. sendTextResponse(w, result)
  1439. } else if key != "" && value != "" {
  1440. //Set mode. Set the preference with given key
  1441. sysdb.Write("fs", "pref/"+key+"/"+username, value)
  1442. sendJSONResponse(w, "\"OK\"")
  1443. }
  1444. }
  1445. func system_fs_removeUserPreferences(username string) {
  1446. entries, err := sysdb.ListTable("fs")
  1447. if err != nil {
  1448. return
  1449. }
  1450. for _, keypairs := range entries {
  1451. if strings.Contains(string(keypairs[0]), "pref/") && strings.Contains(string(keypairs[0]), "/"+username) {
  1452. //Remove this preference
  1453. sysdb.Delete("fs", string(keypairs[0]))
  1454. }
  1455. }
  1456. }
  1457. func system_fs_listDrives(w http.ResponseWriter, r *http.Request) {
  1458. if authAgent.CheckAuth(r) == false {
  1459. sendErrorResponse(w, "User not logged in")
  1460. return
  1461. }
  1462. userinfo, _ := userHandler.GetUserInfoFromRequest(w, r)
  1463. type driveInfo struct {
  1464. Drivepath string
  1465. DriveFreeSpace uint64
  1466. DriveTotalSpace uint64
  1467. DriveAvailSpace uint64
  1468. }
  1469. var drives []driveInfo
  1470. if runtime.GOOS == "windows" {
  1471. //Under windows
  1472. for _, drive := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
  1473. f, err := os.Open(string(drive) + ":\\")
  1474. if err == nil {
  1475. thisdrive := new(driveInfo)
  1476. thisdrive.Drivepath = string(drive) + ":\\"
  1477. free, total, avail := storage.GetDriveCapacity(string(drive) + ":\\")
  1478. thisdrive.DriveFreeSpace = free
  1479. thisdrive.DriveTotalSpace = total
  1480. thisdrive.DriveAvailSpace = avail
  1481. drives = append(drives, *thisdrive)
  1482. f.Close()
  1483. }
  1484. }
  1485. } else {
  1486. //Under linux environment
  1487. //Append all the virtual directories root as root instead
  1488. storageDevices := []string{}
  1489. for _, fshandler := range userinfo.GetAllFileSystemHandler() {
  1490. storageDevices = append(storageDevices, fshandler.Path)
  1491. }
  1492. //List all storage information of each devices
  1493. for _, dev := range storageDevices {
  1494. thisdrive := new(driveInfo)
  1495. thisdrive.Drivepath = filepath.Base(dev)
  1496. free, total, avail := storage.GetDriveCapacity(string(dev))
  1497. thisdrive.DriveFreeSpace = free
  1498. thisdrive.DriveTotalSpace = total
  1499. thisdrive.DriveAvailSpace = avail
  1500. drives = append(drives, *thisdrive)
  1501. }
  1502. }
  1503. jsonString, _ := json.Marshal(drives)
  1504. sendJSONResponse(w, string(jsonString))
  1505. }
  1506. func system_fs_listRoot(w http.ResponseWriter, r *http.Request) {
  1507. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1508. if err != nil {
  1509. sendErrorResponse(w, err.Error())
  1510. return
  1511. }
  1512. username := userinfo.Username
  1513. userRoot, _ := mv(r, "user", false)
  1514. if userRoot == "true" {
  1515. type fileObject struct {
  1516. Filename string
  1517. Filepath string
  1518. IsDir bool
  1519. }
  1520. //List the root media folders under user:/
  1521. var filesInUserRoot []fileObject
  1522. filesInRoot, _ := filepath.Glob(*root_directory + "users/" + username + "/*")
  1523. for _, file := range filesInRoot {
  1524. thisFile := new(fileObject)
  1525. thisFile.Filename = filepath.Base(file)
  1526. thisFile.Filepath, _ = userinfo.RealPathToVirtualPath(file)
  1527. thisFile.IsDir = IsDir(file)
  1528. filesInUserRoot = append(filesInUserRoot, *thisFile)
  1529. }
  1530. jsonString, _ := json.Marshal(filesInUserRoot)
  1531. sendJSONResponse(w, string(jsonString))
  1532. } else {
  1533. type rootObject struct {
  1534. RootName string
  1535. RootPath string
  1536. }
  1537. var roots []rootObject
  1538. for _, store := range userinfo.GetAllFileSystemHandler() {
  1539. var thisDevice = new(rootObject)
  1540. thisDevice.RootName = store.Name
  1541. thisDevice.RootPath = store.UUID + ":/"
  1542. roots = append(roots, *thisDevice)
  1543. }
  1544. jsonString, _ := json.Marshal(roots)
  1545. sendJSONResponse(w, string(jsonString))
  1546. }
  1547. }
  1548. /*
  1549. Special Glob for handling path with [ or ] inside.
  1550. You can also pass in normal path for globing if you are not sure.
  1551. */
  1552. func system_fs_specialGlob(path string) ([]string, error) {
  1553. files, err := filepath.Glob(path)
  1554. if err != nil {
  1555. return []string{}, err
  1556. }
  1557. if strings.Contains(path, "[") == true || strings.Contains(path, "]") == true {
  1558. if len(files) == 0 {
  1559. //Handle reverse check. Replace all [ and ] with *
  1560. newSearchPath := strings.ReplaceAll(path, "[", "?")
  1561. newSearchPath = strings.ReplaceAll(newSearchPath, "]", "?")
  1562. //Scan with all the similar structure except [ and ]
  1563. tmpFilelist, _ := filepath.Glob(newSearchPath)
  1564. for _, file := range tmpFilelist {
  1565. file = filepath.ToSlash(file)
  1566. if strings.Contains(file, filepath.ToSlash(filepath.Dir(path))) {
  1567. files = append(files, file)
  1568. }
  1569. }
  1570. }
  1571. }
  1572. //Convert all filepaths to slash
  1573. for i := 0; i < len(files); i++ {
  1574. files[i] = filepath.ToSlash(files[i])
  1575. }
  1576. return files, nil
  1577. }
  1578. func system_fs_specialURIDecode(inputPath string) string {
  1579. inputPath = strings.ReplaceAll(inputPath, "+", "{{plus_sign}}")
  1580. inputPath, _ = url.QueryUnescape(inputPath)
  1581. inputPath = strings.ReplaceAll(inputPath, "{{plus_sign}}", "+")
  1582. return inputPath
  1583. }
  1584. func system_fs_specialURIEncode(inputPath string) string {
  1585. inputPath = strings.ReplaceAll(inputPath, " ", "{{space_sign}}")
  1586. inputPath, _ = url.QueryUnescape(inputPath)
  1587. inputPath = strings.ReplaceAll(inputPath, "{{space_sign}}", "%20")
  1588. return inputPath
  1589. }
  1590. func system_fs_matchFileExt(inputFilename string, extArray []string) bool {
  1591. inputExt := filepath.Ext(inputFilename)
  1592. if stringInSlice(inputExt, extArray) {
  1593. return true
  1594. }
  1595. return false
  1596. }
  1597. //Handle file properties request
  1598. func system_fs_getFileProperties(w http.ResponseWriter, r *http.Request) {
  1599. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1600. if err != nil {
  1601. sendErrorResponse(w, err.Error())
  1602. return
  1603. }
  1604. vpath, err := mv(r, "path", true)
  1605. if err != nil {
  1606. sendErrorResponse(w, "path not defined")
  1607. return
  1608. }
  1609. rpath, err := userinfo.VirtualPathToRealPath(vpath)
  1610. if err != nil {
  1611. sendErrorResponse(w, err.Error())
  1612. return
  1613. }
  1614. fileStat, err := os.Stat(rpath)
  1615. if err != nil {
  1616. sendErrorResponse(w, err.Error())
  1617. return
  1618. }
  1619. type fileProperties struct {
  1620. VirtualPath string
  1621. StoragePath string
  1622. Basename string
  1623. VirtualDirname string
  1624. StorageDirname string
  1625. Ext string
  1626. MimeType string
  1627. Filesize int64
  1628. Permission string
  1629. LastModTime string
  1630. LastModUnix int64
  1631. IsDirectory bool
  1632. Owner string
  1633. }
  1634. mime := "text/directory"
  1635. if !fileStat.IsDir() {
  1636. m, _, err := fs.GetMime(rpath)
  1637. if err != nil {
  1638. mime = ""
  1639. }
  1640. mime = m
  1641. }
  1642. filesize := fileStat.Size()
  1643. //Get file overall size if this is folder
  1644. if fileStat.IsDir() {
  1645. var size int64
  1646. filepath.Walk(rpath, func(_ string, info os.FileInfo, err error) error {
  1647. if err != nil {
  1648. return err
  1649. }
  1650. if !info.IsDir() {
  1651. size += info.Size()
  1652. }
  1653. return err
  1654. })
  1655. filesize = size
  1656. }
  1657. //Get file owner
  1658. owner := userinfo.GetFileOwner(rpath)
  1659. if owner == "" {
  1660. owner = "Unknown"
  1661. }
  1662. result := fileProperties{
  1663. VirtualPath: vpath,
  1664. StoragePath: filepath.Clean(rpath),
  1665. Basename: filepath.Base(rpath),
  1666. VirtualDirname: filepath.ToSlash(filepath.Dir(vpath)),
  1667. StorageDirname: filepath.ToSlash(filepath.Dir(rpath)),
  1668. Ext: filepath.Ext(rpath),
  1669. MimeType: mime,
  1670. Filesize: filesize,
  1671. Permission: fileStat.Mode().Perm().String(),
  1672. LastModTime: timeToString(fileStat.ModTime()),
  1673. LastModUnix: fileStat.ModTime().Unix(),
  1674. IsDirectory: fileStat.IsDir(),
  1675. Owner: owner,
  1676. }
  1677. jsonString, _ := json.Marshal(result)
  1678. sendJSONResponse(w, string(jsonString))
  1679. }
  1680. /*
  1681. List directory in the given path
  1682. Usage: Pass in dir like the following examples:
  1683. AOR:/Desktop <= Open /user/{username}/Desktop
  1684. S1:/ <= Open {uuid=S1}/
  1685. */
  1686. func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
  1687. currentDir, _ := mv(r, "dir", true)
  1688. //Commented this line to handle dirname that contains "+" sign
  1689. //currentDir, _ = url.QueryUnescape(currentDir)
  1690. sortMode, _ := mv(r, "sort", true)
  1691. showHidden, _ := mv(r, "showHidden", true)
  1692. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1693. if err != nil {
  1694. //user not logged in. Redirect to login page.
  1695. sendErrorResponse(w, "User not logged in")
  1696. return
  1697. }
  1698. if currentDir == "" {
  1699. sendErrorResponse(w, "Invalid dir given.")
  1700. return
  1701. }
  1702. //Pad a slash at the end of currentDir if not exists
  1703. if currentDir[len(currentDir)-1:] != "/" {
  1704. currentDir = currentDir + "/"
  1705. }
  1706. //Convert the virutal path to realpath
  1707. realpath, err := userinfo.VirtualPathToRealPath(currentDir)
  1708. if err != nil {
  1709. sendErrorResponse(w, "Error. Unable to parse path. "+err.Error())
  1710. return
  1711. }
  1712. if !fileExists(realpath) {
  1713. userRoot, _ := userinfo.VirtualPathToRealPath("user:/")
  1714. if filepath.Clean(realpath) == filepath.Clean(userRoot) {
  1715. //Initiate user folder (Initiaed in user object)
  1716. userinfo.GetHomeDirectory()
  1717. } else {
  1718. //Folder not exists
  1719. sendJSONResponse(w, "{\"error\":\"Folder not exists\"}")
  1720. return
  1721. }
  1722. }
  1723. if sortMode == "" {
  1724. sortMode = "default"
  1725. }
  1726. //Check for really special exception in where the path contains [ or ] which cannot be handled via Golang Glob function
  1727. files, _ := system_fs_specialGlob(filepath.Clean(realpath) + "/*")
  1728. var parsedFilelist []fs.FileData
  1729. for _, v := range files {
  1730. if showHidden != "true" && filepath.Base(v)[:1] == "." {
  1731. //Skipping hidden files
  1732. continue
  1733. }
  1734. rawsize := fs.GetFileSize(v)
  1735. modtime, _ := fs.GetModTime(v)
  1736. thisFile := fs.FileData{
  1737. Filename: filepath.Base(v),
  1738. Filepath: currentDir + filepath.Base(v),
  1739. Realpath: v,
  1740. IsDir: IsDir(v),
  1741. Filesize: rawsize,
  1742. Displaysize: fs.GetFileDisplaySize(rawsize, 2),
  1743. ModTime: modtime,
  1744. IsShared: shareManager.FileIsShared(v),
  1745. }
  1746. parsedFilelist = append(parsedFilelist, thisFile)
  1747. }
  1748. //Sort the filelist
  1749. if sortMode == "default" {
  1750. //Sort by name, convert filename to window sorting methods
  1751. sort.Slice(parsedFilelist, func(i, j int) bool {
  1752. return strings.ToLower(parsedFilelist[i].Filename) < strings.ToLower(parsedFilelist[j].Filename)
  1753. })
  1754. } else if sortMode == "reverse" {
  1755. //Sort by reverse name
  1756. sort.Slice(parsedFilelist, func(i, j int) bool {
  1757. return strings.ToLower(parsedFilelist[i].Filename) > strings.ToLower(parsedFilelist[j].Filename)
  1758. })
  1759. } else if sortMode == "smallToLarge" {
  1760. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize < parsedFilelist[j].Filesize })
  1761. } else if sortMode == "largeToSmall" {
  1762. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize > parsedFilelist[j].Filesize })
  1763. } else if sortMode == "mostRecent" {
  1764. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime > parsedFilelist[j].ModTime })
  1765. } else if sortMode == "leastRecent" {
  1766. sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime < parsedFilelist[j].ModTime })
  1767. }
  1768. jsonString, _ := json.Marshal(parsedFilelist)
  1769. sendJSONResponse(w, string(jsonString))
  1770. }
  1771. //Handle getting a hash from a given contents in the given path
  1772. func system_fs_handleDirHash(w http.ResponseWriter, r *http.Request) {
  1773. currentDir, err := mv(r, "dir", true)
  1774. if err != nil {
  1775. sendErrorResponse(w, "Invalid dir given")
  1776. return
  1777. }
  1778. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1779. if err != nil {
  1780. sendErrorResponse(w, "User not logged in")
  1781. return
  1782. }
  1783. rpath, err := userinfo.VirtualPathToRealPath(currentDir)
  1784. if err != nil {
  1785. sendErrorResponse(w, "Invalid dir given")
  1786. return
  1787. }
  1788. //Get a list of files in this directory
  1789. currentDir = filepath.ToSlash(filepath.Clean(rpath)) + "/"
  1790. filesInDir, err := system_fs_specialGlob(currentDir + "*")
  1791. if err != nil {
  1792. sendErrorResponse(w, err.Error())
  1793. return
  1794. }
  1795. filenames := []string{}
  1796. for _, file := range filesInDir {
  1797. if len(filepath.Base(file)) > 0 && string([]rune(filepath.Base(file))[0]) != "." {
  1798. //Ignore hidden files
  1799. filenames = append(filenames, filepath.Base(file))
  1800. }
  1801. }
  1802. sort.Strings(filenames)
  1803. //Build a hash base on the filelist
  1804. h := sha256.New()
  1805. h.Write([]byte(strings.Join(filenames, ",")))
  1806. sendTextResponse(w, hex.EncodeToString((h.Sum(nil))))
  1807. }
  1808. /*
  1809. File zipping and unzipping functions
  1810. */
  1811. //Handle all zip related API
  1812. func system_fs_zipHandler(w http.ResponseWriter, r *http.Request) {
  1813. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1814. if err != nil {
  1815. sendErrorResponse(w, err.Error())
  1816. return
  1817. }
  1818. opr, err := mv(r, "opr", true)
  1819. if err != nil {
  1820. sendErrorResponse(w, "Invalid opr or opr not defined")
  1821. return
  1822. }
  1823. vsrc, _ := mv(r, "src", true)
  1824. if vsrc == "" {
  1825. sendErrorResponse(w, "Invalid src paramter")
  1826. return
  1827. }
  1828. vdest, _ := mv(r, "dest", true)
  1829. rdest := ""
  1830. //Convert source path from JSON string to object
  1831. virtualSourcePaths := []string{}
  1832. err = json.Unmarshal([]byte(vsrc), &virtualSourcePaths)
  1833. if err != nil {
  1834. sendErrorResponse(w, err.Error())
  1835. return
  1836. }
  1837. //Check each of the path
  1838. realSourcePaths := []string{}
  1839. for _, vpath := range virtualSourcePaths {
  1840. thisrpath, err := userinfo.VirtualPathToRealPath(vpath)
  1841. if err != nil || !fileExists(thisrpath) {
  1842. sendErrorResponse(w, "File not exists: "+vpath)
  1843. return
  1844. }
  1845. realSourcePaths = append(realSourcePaths, thisrpath)
  1846. }
  1847. ///Convert dest to real if given
  1848. if vdest != "" {
  1849. realdest, _ := userinfo.VirtualPathToRealPath(vdest)
  1850. rdest = realdest
  1851. }
  1852. //This function will be deprecate soon in ArozOS 1.120
  1853. log.Println("*DEPRECATE* zipHandler will be deprecating soon! Please use fileOpr endpoint")
  1854. if opr == "zip" {
  1855. //Check if destination location exists
  1856. if rdest == "" || !fileExists(filepath.Dir(rdest)) {
  1857. sendErrorResponse(w, "Invalid dest location")
  1858. return
  1859. }
  1860. //OK. Create the zip at the desired location
  1861. err := fs.ArozZipFile(realSourcePaths, rdest, false)
  1862. if err != nil {
  1863. sendErrorResponse(w, err.Error())
  1864. return
  1865. }
  1866. sendOK(w)
  1867. } else if opr == "tmpzip" {
  1868. //Zip to tmp folder
  1869. userTmpFolder, _ := userinfo.VirtualPathToRealPath("tmp:/")
  1870. filename := Int64ToString(GetUnixTime()) + ".zip"
  1871. rdest := filepath.ToSlash(filepath.Clean(userTmpFolder)) + "/" + filename
  1872. log.Println(realSourcePaths, rdest)
  1873. err := fs.ArozZipFile(realSourcePaths, rdest, false)
  1874. if err != nil {
  1875. sendErrorResponse(w, err.Error())
  1876. return
  1877. }
  1878. //Send the tmp filename to the user
  1879. sendTextResponse(w, "tmp:/"+filename)
  1880. } else if opr == "inspect" {
  1881. } else if opr == "unzip" {
  1882. }
  1883. }
  1884. //Translate path from and to virtual and realpath
  1885. func system_fs_handlePathTranslate(w http.ResponseWriter, r *http.Request) {
  1886. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1887. if err != nil {
  1888. sendErrorResponse(w, err.Error())
  1889. return
  1890. }
  1891. path, err := mv(r, "path", false)
  1892. if err != nil {
  1893. sendErrorResponse(w, "Invalid path given")
  1894. return
  1895. }
  1896. rpath, err := userinfo.VirtualPathToRealPath(path)
  1897. if err != nil {
  1898. //Try to convert it to virtualPath
  1899. vpath, err := userinfo.RealPathToVirtualPath(path)
  1900. if err != nil {
  1901. sendErrorResponse(w, "Unknown path given")
  1902. } else {
  1903. jsonstring, _ := json.Marshal(vpath)
  1904. sendJSONResponse(w, string(jsonstring))
  1905. }
  1906. } else {
  1907. abrpath, _ := filepath.Abs(rpath)
  1908. jsonstring, _ := json.Marshal([]string{rpath, filepath.ToSlash(abrpath)})
  1909. sendJSONResponse(w, string(jsonstring))
  1910. }
  1911. }
  1912. //Handle cache rendering with websocket pipeline
  1913. func system_fs_handleCacheRender(w http.ResponseWriter, r *http.Request) {
  1914. userinfo, _ := userHandler.GetUserInfoFromRequest(w, r)
  1915. vpath, err := mv(r, "folder", false)
  1916. if err != nil {
  1917. sendErrorResponse(w, "Invalid folder paramter")
  1918. return
  1919. }
  1920. //Convert vpath to realpath
  1921. rpath, err := userinfo.VirtualPathToRealPath(vpath)
  1922. if err != nil {
  1923. sendErrorResponse(w, err.Error())
  1924. return
  1925. }
  1926. //Perform cache rendering
  1927. thumbRenderHandler.HandleLoadCache(w, r, rpath)
  1928. }
  1929. //Handle file thumbnail caching
  1930. func system_fs_handleFolderCache(w http.ResponseWriter, r *http.Request) {
  1931. userinfo, _ := userHandler.GetUserInfoFromRequest(w, r)
  1932. vfolderpath, err := mv(r, "folder", false)
  1933. if err != nil {
  1934. sendErrorResponse(w, "folder not defined")
  1935. return
  1936. }
  1937. rpath, err := userinfo.VirtualPathToRealPath(vfolderpath)
  1938. if err != nil {
  1939. sendErrorResponse(w, err.Error())
  1940. return
  1941. }
  1942. thumbRenderHandler.BuildCacheForFolder(rpath)
  1943. sendOK(w)
  1944. }
  1945. //Functions for handling quick file write without the need to go through agi for simple apps
  1946. func system_fs_handleFileWrite(w http.ResponseWriter, r *http.Request) {
  1947. //Get the username for this user
  1948. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1949. if err != nil {
  1950. sendErrorResponse(w, err.Error())
  1951. return
  1952. }
  1953. //Get the file content and the filepath
  1954. content, _ := mv(r, "content", true)
  1955. targetFilepath, err := mv(r, "filepath", true)
  1956. if err != nil {
  1957. sendErrorResponse(w, "Filepath cannot be empty")
  1958. return
  1959. }
  1960. //Check if filepath is writable
  1961. if !userinfo.CanWrite(targetFilepath) {
  1962. sendErrorResponse(w, "Write permission denied")
  1963. return
  1964. }
  1965. //Convert the filepath to realpath
  1966. rpath, err := userinfo.VirtualPathToRealPath(targetFilepath)
  1967. if err != nil {
  1968. sendErrorResponse(w, err.Error())
  1969. return
  1970. }
  1971. //Check if the path dir exists. If not, return error
  1972. if !fileExists(filepath.Dir(rpath)) {
  1973. sendErrorResponse(w, "Directory not exists")
  1974. return
  1975. }
  1976. //OK. Write to that file
  1977. err = ioutil.WriteFile(rpath, []byte(content), 0755)
  1978. if err != nil {
  1979. sendErrorResponse(w, err.Error())
  1980. return
  1981. }
  1982. sendOK(w)
  1983. }
  1984. //Handle setting and loading of file permission on Linux
  1985. func system_fs_handleFilePermission(w http.ResponseWriter, r *http.Request) {
  1986. file, err := mv(r, "file", true)
  1987. if err != nil {
  1988. sendErrorResponse(w, "Invalid file")
  1989. return
  1990. }
  1991. //Translate the file to real path
  1992. userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
  1993. if err != nil {
  1994. sendErrorResponse(w, "User not logged in")
  1995. return
  1996. }
  1997. rpath, err := userinfo.VirtualPathToRealPath(file)
  1998. if err != nil {
  1999. sendErrorResponse(w, err.Error())
  2000. return
  2001. }
  2002. newMode, _ := mv(r, "mode", true)
  2003. if newMode == "" {
  2004. //Read the file mode
  2005. //Check if the file exists
  2006. if !fileExists(rpath) {
  2007. sendErrorResponse(w, "File not exists!")
  2008. return
  2009. }
  2010. //Read the file permission
  2011. filePermission, err := fsp.GetFilePermissions(rpath)
  2012. if err != nil {
  2013. sendErrorResponse(w, err.Error())
  2014. return
  2015. }
  2016. //Send the file permission to client
  2017. js, _ := json.Marshal(filePermission)
  2018. sendJSONResponse(w, string(js))
  2019. } else {
  2020. //Set the file mode
  2021. //Check if the file exists
  2022. if !fileExists(rpath) {
  2023. sendErrorResponse(w, "File not exists!")
  2024. return
  2025. }
  2026. //Check if windows. If yes, ignore this request
  2027. if runtime.GOOS == "windows" {
  2028. sendErrorResponse(w, "Windows host not supported")
  2029. return
  2030. }
  2031. //Check if this user has permission to change the file permission
  2032. //Aka user must be 1. This is his own folder or 2. Admin
  2033. fsh, _ := userinfo.GetFileSystemHandlerFromVirtualPath(file)
  2034. if fsh.Hierarchy == "user" {
  2035. //Always ok as this is owned by the user
  2036. } else if fsh.Hierarchy == "public" {
  2037. //Require admin
  2038. if userinfo.IsAdmin() == false {
  2039. sendErrorResponse(w, "Permission Denied")
  2040. return
  2041. }
  2042. } else {
  2043. //Not implemeneted. Require admin
  2044. if userinfo.IsAdmin() == false {
  2045. sendErrorResponse(w, "Permission Denied")
  2046. return
  2047. }
  2048. }
  2049. //Be noted that if the system is not running in sudo mode,
  2050. //File permission change might not works.
  2051. err := fsp.SetFilePermisson(rpath, newMode)
  2052. if err != nil {
  2053. sendErrorResponse(w, err.Error())
  2054. return
  2055. } else {
  2056. sendOK(w)
  2057. }
  2058. }
  2059. }
  2060. //Check if the given filepath is and must inside the given directory path.
  2061. //You can pass both as relative
  2062. func system_fs_checkFileInDirectory(filesourcepath string, directory string) bool {
  2063. filepathAbs, err := filepath.Abs(filesourcepath)
  2064. if err != nil {
  2065. return false
  2066. }
  2067. directoryAbs, err := filepath.Abs(directory)
  2068. if err != nil {
  2069. return false
  2070. }
  2071. //Check if the filepathabs contain directoryAbs
  2072. if strings.Contains(filepathAbs, directoryAbs) {
  2073. return true
  2074. } else {
  2075. return false
  2076. }
  2077. }
  2078. //Clear the old files inside the tmp file
  2079. func system_fs_clearOldTmpFiles() {
  2080. filesToBeDelete := []string{}
  2081. tmpAbs, _ := filepath.Abs(*tmp_directory)
  2082. filepath.Walk(*tmp_directory, func(path string, info os.FileInfo, err error) error {
  2083. if filepath.Base(path) != "aofs.db" && filepath.Base(path) != "aofs.db.lock" {
  2084. //Check if root folders. Do not delete root folders
  2085. parentAbs, _ := filepath.Abs(filepath.Dir(path))
  2086. if tmpAbs == parentAbs {
  2087. //Root folder. Do not remove
  2088. return nil
  2089. }
  2090. //Get its modification time
  2091. modTime, err := fs.GetModTime(path)
  2092. if err != nil {
  2093. return nil
  2094. }
  2095. //Check if mod time is more than 24 hours ago
  2096. if time.Now().Unix()-modTime > int64(*maxTempFileKeepTime) {
  2097. //Delete OK
  2098. filesToBeDelete = append(filesToBeDelete, path)
  2099. }
  2100. }
  2101. return nil
  2102. })
  2103. //Remove all files from the delete list
  2104. for _, fileToBeDelete := range filesToBeDelete {
  2105. os.RemoveAll(fileToBeDelete)
  2106. }
  2107. }