file_system.go 68 KB

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