file_system.go 69 KB

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