file_system.go 69 KB

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