file_system.go 73 KB

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