file_system.go 78 KB

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