Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mports System
  2. Imports System.Collections.Generic
  3. Imports System.Globalization
  4. Imports System.Linq
  5. Imports System.Threading.Tasks
  6. Imports Microsoft.AspNetCore.Mvc
  7. Imports System.IO
  8. Imports Microsoft.AspNetCore.Hosting
  9. Imports Microsoft.AspNetCore.Http
  10. Imports System.Net.Mime
  11.  
  12. Namespace API.NetCore.Controllers
  13.     <Route("[controller]")>
  14.     Public Class FileManagerController
  15.         Inherits Controller
  16.  
  17.         Private ReadOnly _webRootPath As String
  18.         Private ReadOnly _webPath As String
  19.         Private ReadOnly _allowedExtensions As List(Of String)
  20.  
  21.         Public Sub New(ByVal env As IHostingEnvironment)
  22.             _webPath = "ContentLibrary"
  23.  
  24.             If String.IsNullOrWhiteSpace(env.WebRootPath) Then
  25.                 env.WebRootPath = Directory.GetCurrentDirectory()
  26.             End If
  27.  
  28.             _webRootPath = Path.Combine(env.WebRootPath, _webPath)
  29.             _allowedExtensions = New List(Of String) From {"jpg", "jpe", "jpeg", "gif", "png", "svg", "txt", "pdf", "odp", "ods", "odt", "rtf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "csv", "ogv", "avi", "mkv", "mp4", "webm", "m4v", "ogg", "mp3", "wav", "zip", "rar", "md"}
  30.             }
  31.         End Sub
  32.  
  33.         Public Function Index(ByVal mode As String, ByVal path As String, ByVal name As String, ByVal files As List(Of IFormFile), ByVal old As String, ByVal [new] As String, ByVal source As String, ByVal target As String, ByVal content As String, ByVal thumbnail As Boolean, ByVal string As String) As IActionResult
  34.             Try
  35.  
  36.                 If mode Is Nothing Then
  37.                     Return Nothing
  38.                 End If
  39.  
  40.                 If Not String.IsNullOrWhiteSpace(path) AndAlso path.StartsWith("/") Then path = path.Substring(1)
  41.                 If Not String.IsNullOrWhiteSpace([new]) AndAlso [new].StartsWith("/") Then [new] = If([new] = "/", String.Empty, [new].Substring(1))
  42.                 If Not String.IsNullOrWhiteSpace(source) AndAlso source.StartsWith("/") Then source = source.Substring(1)
  43.                 If Not String.IsNullOrWhiteSpace(target) AndAlso target.StartsWith("/") Then target = target.Substring(1)
  44.  
  45.                 Select Case mode.ToLower(CultureInfo.CurrentCulture)
  46.                     Case "initiate"
  47.                         Return Json(Initiate())
  48.                     Case "getinfo"
  49.                         Return Json(GetInfo(path))
  50.                     Case "readfolder"
  51.                         Return Json(ReadFolder(path))
  52.                     Case "addfolder"
  53.                         Return Json(AddFolder(path, name))
  54.                     Case "upload"
  55.                         Return Json(Upload(path, files).Result)
  56.                     Case "rename"
  57.                         Return Json(Rename(old, [new]))
  58.                     Case "move"
  59.                         Return Json(Move(old, [new]))
  60.                     Case "copy"
  61.                         Return Json(Copy(source, target))
  62.                     Case "savefile"
  63.                         Return Json(SaveFile(path, content))
  64.                     Case "delete"
  65.                         Return Json(Delete(path))
  66.                     Case "download"
  67.                         Return Download(path)
  68.                     Case "getimage"
  69.                         Return GetImage(path, thumbnail)
  70.                     Case "readfile"
  71.                         Return ReadFile(path)
  72.                     Case "summarize"
  73.                         Return Json(Summarize())
  74.                     Case "seekfolder"
  75.                         Return Json(SeekFolder(path, string))
  76.                 End Select
  77.  
  78.                 Throw New Exception("Unknown Request!")
  79.             Catch e As Exception
  80.                 Return New JsonResult(e.Message) With {
  81.                     .StatusCode = StatusCodes.Status500InternalServerError,
  82.                     .ContentType = "application/json"
  83.                 }
  84.             End Try
  85.         End Function
  86.  
  87.         Private Function Initiate() As dynamic
  88.             Dim result = New With {Key
  89.                 .Data = New With {Key
  90.                     .Type = "initiate", Key
  91.                     .Attributes = New With {Key
  92.                         .Config = New With {Key
  93.                             .Security = New With {Key
  94.                                 .[ReadOnly] = False, Key
  95.                                 .Extensions = New With {Key
  96.                                     .IgnoreCase = True, Key
  97.                                     .Policy = "ALLOW_LIST", Key
  98.                                     .Restrictions = _allowedExtensions
  99.                                 }
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.             Return result
  106.         End Function
  107.  
  108.         Private Function GetUnixTimestamp(ByVal dt As DateTime) As Int32
  109.             Return CInt((dt.ToUniversalTime().Subtract(New DateTime(1970, 1, 1))).TotalSeconds)
  110.         End Function
  111.  
  112.         Private Function SeekFolder(ByVal path As String, ByVal search As String) As dynamic
  113.             If path Is Nothing Then
  114.                 path = String.Empty
  115.             End If
  116.  
  117.             Dim searchPath = Path.Combine(_webRootPath, path)
  118.             Dim data = New List(Of dynamic)()
  119.  
  120.             For Each file As FileInfo In New DirectoryInfo(searchPath).GetFiles("*" & search & "*", SearchOption.AllDirectories)
  121.                 Dim item = New With {Key
  122.                     .Id = MakeWebPath(Path.Combine(Path.GetRelativePath(_webRootPath, file.DirectoryName), file.Name), True), Key
  123.                     .Type = "file", Key
  124.                     .Attributes = New With {Key
  125.                         .Name = file.Name, Key
  126.                         .Path = MakeWebPath(Path.Combine(Path.GetRelativePath(_webRootPath, file.DirectoryName), file.Name), True), Key
  127.                         .Readable = 1, Key
  128.                         .Writable = 1, Key
  129.                         .Created = GetUnixTimestamp(file.CreationTimeUtc), Key
  130.                         .Modified = GetUnixTimestamp(file.LastWriteTimeUtc), Key
  131.                         .Size = file.Length, Key
  132.                         .Extension = file.Extension.TrimStart("."c), Key
  133.                         .Timestamp = DateTime.Now.Subtract(file.LastWriteTime).TotalSeconds
  134.                     }
  135.                 }
  136.                 data.Add(item)
  137.             Next
  138.  
  139.             For Each dir As DirectoryInfo In New DirectoryInfo(searchPath).GetDirectories("*" & search & "*", SearchOption.AllDirectories)
  140.                 Dim item = New With {Key
  141.                     .Id = MakeWebPath(Path.GetRelativePath(_webRootPath, dir.FullName), False, True), Key
  142.                     .Type = "folder", Key
  143.                     .Attributes = New With {Key
  144.                         .Name = dir.Name, Key
  145.                         .Path = MakeWebPath(dir.FullName, True, True), Key
  146.                         .Readable = 1, Key
  147.                         .Writable = 1, Key
  148.                         .Created = GetUnixTimestamp(dir.CreationTimeUtc), Key
  149.                         .Modified = GetUnixTimestamp(dir.LastWriteTimeUtc), Key
  150.                         .Timestamp = DateTime.Now.Subtract(dir.LastWriteTime).TotalSeconds
  151.                     }
  152.                 }
  153.                 data.Add(item)
  154.             Next
  155.  
  156.             Return New With {Key
  157.                 .Data = data
  158.             }
  159.         End Function
  160.  
  161.         Private Function GetInfo(ByVal path As String) As dynamic
  162.             If path Is Nothing Then
  163.                 path = String.Empty
  164.             End If
  165.  
  166.             Dim filePath = Path.Combine(_webRootPath, path)
  167.             Dim file As FileInfo = New FileInfo(path)
  168.             Return New With {Key
  169.                 .Data = New With {Key
  170.                     .Id = MakeWebPath(Path.Combine(Path.GetRelativePath(_webRootPath, file.DirectoryName), file.Name), True), Key
  171.                     .Type = "file", Key
  172.                     .Attributes = New With {Key
  173.                         .Name = file.Name, Key
  174.                         .Path = MakeWebPath(Path.Combine(Path.GetRelativePath(_webRootPath, file.DirectoryName), file.Name), False), Key
  175.                         .Readable = 1, Key
  176.                         .Writable = 1, Key
  177.                         .Created = GetUnixTimestamp(file.CreationTimeUtc), Key
  178.                         .Modified = GetUnixTimestamp(file.LastWriteTimeUtc), Key
  179.                         .Size = file.Length, Key
  180.                         .Extension = file.Extension.TrimStart("."c), Key
  181.                         .Timestamp = DateTime.Now.Subtract(file.LastWriteTime).TotalSeconds
  182.                     }
  183.                 }
  184.             }
  185.         End Function
  186.  
  187.         Private Function ReadFolder(ByVal path As String) As dynamic
  188.             If path Is Nothing Then path = String.Empty
  189.             Dim rootpath = Path.Combine(_webRootPath, path)
  190.             Dim rootDirectory = New DirectoryInfo(rootpath)
  191.             Dim data = New List(Of dynamic)()
  192.  
  193.             For Each directory In rootDirectory.GetDirectories()
  194.                 Dim item = New With {Key
  195.                     .Id = MakeWebPath(Path.Combine(path, directory.Name), False, True), Key
  196.                     .Type = "folder", Key
  197.                     .Attributes = New With {Key
  198.                         .Name = directory.Name, Key
  199.                         .Path = MakeWebPath(Path.Combine(_webPath, path, directory.Name), True, True), Key
  200.                         .Readable = 1, Key
  201.                         .Writable = 1, Key
  202.                         .Created = GetUnixTimestamp(directory.CreationTime), Key
  203.                         .Modified = GetUnixTimestamp(directory.LastWriteTime), Key
  204.                         .Timestamp = DateTime.Now.Subtract(directory.LastWriteTime).TotalSeconds
  205.                     }
  206.                 }
  207.                 data.Add(item)
  208.             Next
  209.  
  210.             For Each file In rootDirectory.GetFiles()
  211.                 Dim item = New With {Key
  212.                     .Id = MakeWebPath(Path.Combine(path, file.Name)), Key
  213.                     .Type = "file", Key
  214.                     .Attributes = New With {Key
  215.                         .Name = file.Name, Key
  216.                         .Path = MakeWebPath(Path.Combine(_webPath, path, file.Name), True), Key
  217.                         .Readable = 1, Key
  218.                         .Writable = 1, Key
  219.                         .Created = GetUnixTimestamp(file.CreationTime), Key
  220.                         .Modified = GetUnixTimestamp(file.LastWriteTime), Key
  221.                         .Extension = file.Extension.Replace(".", ""), Key
  222.                         .Size = file.Length, Key
  223.                         .Timestamp = DateTime.Now.Subtract(file.LastWriteTime).TotalSeconds
  224.                     }
  225.                 }
  226.                 data.Add(item)
  227.             Next
  228.  
  229.             Dim result = New With {Key
  230.                 .Data = data
  231.             }
  232.             Return result
  233.         End Function
  234.  
  235.         Private Function AddFolder(ByVal path As String, ByVal name As String) As dynamic
  236.             Dim newDirectoryPath = Path.Combine(_webRootPath, path, name)
  237.             Dim directoryExist = Directory.Exists(newDirectoryPath)
  238.  
  239.             If directoryExist Then
  240.                 Dim errorResult = New With {Key
  241.                     .Errors = New List(Of dynamic)()
  242.                 }
  243.                 errorResult.Errors.Add(New With {Key
  244.                     .Code = "500", Key
  245.                     .Title = "DIRECTORY_ALREADY_EXISTS", Key
  246.                     .Meta = New With {Key
  247.                         .Arguments = New List(Of String) From {
  248.                             name
  249.                         }
  250.                     }
  251.                 })
  252.                 Return errorResult
  253.             End If
  254.  
  255.             Directory.CreateDirectory(newDirectoryPath)
  256.             Dim directory = New DirectoryInfo(newDirectoryPath)
  257.             Dim result = New With {Key
  258.                 .Data = New With {Key
  259.                     .Id = MakeWebPath(Path.Combine(path, directory.Name), False, True), Key
  260.                     .Type = "folder", Key
  261.                     .Attributes = New With {Key
  262.                         .Name = directory.Name, Key
  263.                         .Path = MakeWebPath(Path.Combine(_webPath, path, directory.Name), True, True), Key
  264.                         .Readable = 1, Key
  265.                         .Writable = 1, Key
  266.                         .Created = GetUnixTimestamp(DateTime.Now), Key
  267.                         .Modified = GetUnixTimestamp(DateTime.Now)
  268.                     }
  269.                 }
  270.             }
  271.             Return result
  272.         End Function
  273.  
  274.         Private Async Function Upload(ByVal path As String, ByVal files As IEnumerable(Of IFormFile)) As Task(Of dynamic)
  275.             Dim result = New With {Key
  276.                 .Data = New List(Of dynamic)()
  277.             }
  278.  
  279.             For Each file In files
  280.                 If file.Length <= 0 Then Continue For
  281.                 Dim fileExist = System.IO.File.Exists(Path.Combine(_webRootPath, path, file.FileName))
  282.  
  283.                 If fileExist Then
  284.                     Dim errorResult = New With {Key
  285.                         .Errors = New List(Of dynamic)()
  286.                     }
  287.                     errorResult.Errors.Add(New With {Key
  288.                         .Code = "500", Key
  289.                         .Title = "FILE_ALREADY_EXISTS", Key
  290.                         .Meta = New With {Key
  291.                             .Arguments = New List(Of String) From {
  292.                                 file.FileName
  293.                             }
  294.                         }
  295.                     })
  296.                     Return errorResult
  297.                 End If
  298.  
  299.                 Using fileStream = New FileStream(Path.Combine(_webRootPath, path, file.FileName), FileMode.Create)
  300.                     Await file.CopyToAsync(fileStream)
  301.                 End Using
  302.  
  303.                 result.Data.Add(New With {Key
  304.                     .Id = MakeWebPath(Path.Combine(path, file.FileName)), Key
  305.                     .Type = "file", Key
  306.                     .Attributes = New With {Key
  307.                         .Name = file.FileName, Key
  308.                         .Extension = Path.GetExtension(file.FileName).Replace(".", ""), Key
  309.                         .Path = MakeWebPath(Path.Combine(_webPath, path, file.FileName), True), Key
  310.                         .Readable = 1, Key
  311.                         .Writable = 1, Key
  312.                         .Created = GetUnixTimestamp(DateTime.Now), Key
  313.                         .Modified = GetUnixTimestamp(DateTime.Now), Key
  314.                         .Size = file.Length
  315.                     }
  316.                 })
  317.             Next
  318.  
  319.             Return result
  320.         End Function
  321.  
  322.         Private Function Rename(ByVal old As String, ByVal [new] As String) As dynamic
  323.             Dim oldPath = Path.Combine(_webRootPath, old)
  324.             Dim fileAttributes = System.IO.File.GetAttributes(oldPath)
  325.  
  326.             If (fileAttributes And FileAttributes.Directory) = FileAttributes.Directory Then
  327.                 Dim oldDirectoryName = Path.GetDirectoryName(old).Split("\"c).Last()
  328.                 Dim newDirectoryPath = old.Replace(oldDirectoryName, [new])
  329.                 Dim newPath = Path.Combine(_webRootPath, newDirectoryPath)
  330.                 Dim directoryExist = Directory.Exists(newPath)
  331.  
  332.                 If directoryExist Then
  333.                     Dim errorResult = New With {Key
  334.                         .Errors = New List(Of dynamic)()
  335.                     }
  336.                     errorResult.Errors.Add(New With {Key
  337.                         .Code = "500", Key
  338.                         .Title = "DIRECTORY_ALREADY_EXISTS", Key
  339.                         .Meta = New With {Key
  340.                             .Arguments = New List(Of String) From {
  341.                                 [new]
  342.                             }
  343.                         }
  344.                     })
  345.                     Return errorResult
  346.                 End If
  347.  
  348.                 Directory.Move(oldPath, newPath)
  349.                 Dim result = New With {Key
  350.                     .Data = New With {Key
  351.                         .Id = newDirectoryPath, Key
  352.                         .Type = "folder", Key
  353.                         .Attributes = New With {Key
  354.                             .Name = [new], Key
  355.                             .Readable = 1, Key
  356.                             .Writable = 1, Key
  357.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  358.                             .Modified = GetUnixTimestamp(DateTime.Now)
  359.                         }
  360.                     }
  361.                 }
  362.                 Return result
  363.             Else
  364.                 Dim oldFileName = Path.GetFileName(old)
  365.                 Dim newFilePath = old.Replace(oldFileName, [new])
  366.                 Dim newPath = Path.Combine(_webRootPath, newFilePath)
  367.                 Dim fileExist = System.IO.File.Exists(newPath)
  368.  
  369.                 If fileExist Then
  370.                     Dim errorResult = New With {Key
  371.                         .Errors = New List(Of dynamic)()
  372.                     }
  373.                     errorResult.Errors.Add(New With {Key
  374.                         .Code = "500", Key
  375.                         .Title = "FILE_ALREADY_EXISTS", Key
  376.                         .Meta = New With {Key
  377.                             .Arguments = New List(Of String) From {
  378.                                 [new]
  379.                             }
  380.                         }
  381.                     })
  382.                     Return errorResult
  383.                 End If
  384.  
  385.                 System.IO.File.Move(oldPath, newPath)
  386.                 Dim result = New With {Key
  387.                     .Data = New With {Key
  388.                         .Id = newFilePath, Key
  389.                         .Type = "file", Key
  390.                         .Attributes = New With {Key
  391.                             .Name = [new], Key
  392.                             .Extension = Path.GetExtension(newPath).Replace(".", ""), Key
  393.                             .Readable = 1, Key
  394.                             .Writable = 1, Key
  395.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  396.                             .Modified = GetUnixTimestamp(DateTime.Now)
  397.                         }
  398.                     }
  399.                 }
  400.                 Return result
  401.             End If
  402.         End Function
  403.  
  404.         Private Function Move(ByVal old As String, ByVal [new] As String) As dynamic
  405.             Dim fileAttributes = System.IO.File.GetAttributes(Path.Combine(_webRootPath, old))
  406.  
  407.             If (fileAttributes And FileAttributes.Directory) = FileAttributes.Directory Then
  408.                 Dim directoryName = Path.GetDirectoryName(old).Split("\"c).Last()
  409.                 Dim newDirectoryPath = Path.Combine([new], directoryName)
  410.                 Dim oldPath = Path.Combine(_webRootPath, old)
  411.                 Dim newPath = Path.Combine(_webRootPath, [new], directoryName)
  412.                 Dim directoryExist = Directory.Exists(newPath)
  413.  
  414.                 If directoryExist Then
  415.                     Dim errorResult = New With {Key
  416.                         .Errors = New List(Of dynamic)()
  417.                     }
  418.                     errorResult.Errors.Add(New With {Key
  419.                         .Code = "500", Key
  420.                         .Title = "DIRECTORY_ALREADY_EXISTS", Key
  421.                         .Meta = New With {Key
  422.                             .Arguments = New List(Of String) From {
  423.                                 directoryName
  424.                             }
  425.                         }
  426.                     })
  427.                     Return errorResult
  428.                 End If
  429.  
  430.                 Directory.Move(oldPath, newPath)
  431.                 Dim result = New With {Key
  432.                     .Data = New With {Key
  433.                         .Id = newDirectoryPath, Key
  434.                         .Type = "folder", Key
  435.                         .Attributes = New With {Key
  436.                             .Name = directoryName, Key
  437.                             .Readable = 1, Key
  438.                             .Writable = 1, Key
  439.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  440.                             .Modified = GetUnixTimestamp(DateTime.Now)
  441.                         }
  442.                     }
  443.                 }
  444.                 Return result
  445.             Else
  446.                 Dim fileName = Path.GetFileName(old)
  447.                 Dim newFilePath = Path.Combine([new], fileName)
  448.                 Dim oldPath = Path.Combine(_webRootPath, old)
  449.                 Dim newPath = If([new] = "/", Path.Combine(_webRootPath, fileName.Replace("/", "")), Path.Combine(_webRootPath, [new], fileName))
  450.                 Dim fileExist = System.IO.File.Exists(newPath)
  451.  
  452.                 If fileExist Then
  453.                     Dim errorResult = New With {Key
  454.                         .Errors = New List(Of dynamic)()
  455.                     }
  456.                     errorResult.Errors.Add(New With {Key
  457.                         .Code = "500", Key
  458.                         .Title = "FILE_ALREADY_EXISTS", Key
  459.                         .Meta = New With {Key
  460.                             .Arguments = New List(Of String) From {
  461.                                 fileName
  462.                             }
  463.                         }
  464.                     })
  465.                     Return errorResult
  466.                 End If
  467.  
  468.                 System.IO.File.Move(oldPath, newPath)
  469.                 Dim result = New With {Key
  470.                     .Data = New With {Key
  471.                         .Id = newFilePath, Key
  472.                         .Type = "file", Key
  473.                         .Attributes = New With {Key
  474.                             .Name = fileName, Key
  475.                             .Extension = Path.GetExtension([new]).Replace(".", ""), Key
  476.                             .Readable = 1, Key
  477.                             .Writable = 1, Key
  478.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  479.                             .Modified = GetUnixTimestamp(DateTime.Now)
  480.                         }
  481.                     }
  482.                 }
  483.                 Return result
  484.             End If
  485.         End Function
  486.  
  487.         Private Function Copy(ByVal source As String, ByVal target As String) As dynamic
  488.             Dim fileAttributes = System.IO.File.GetAttributes(Path.Combine(_webRootPath, source))
  489.  
  490.             If (fileAttributes And FileAttributes.Directory) = FileAttributes.Directory Then
  491.                 Dim directoryName = Path.GetDirectoryName(source).Split("\"c).Last()
  492.                 Dim newDirectoryPath = Path.Combine(target, directoryName)
  493.                 Dim oldPath = Path.Combine(_webRootPath, source)
  494.                 Dim newPath = Path.Combine(_webRootPath, target, directoryName)
  495.                 Dim directoryExist = Directory.Exists(newPath)
  496.  
  497.                 If directoryExist Then
  498.                     Dim errorResult = New With {Key
  499.                         .Errors = New List(Of dynamic)()
  500.                     }
  501.                     errorResult.Errors.Add(New With {Key
  502.                         .Code = "500", Key
  503.                         .Title = "DIRECTORY_ALREADY_EXISTS", Key
  504.                         .Meta = New With {Key
  505.                             .Arguments = New List(Of String) From {
  506.                                 directoryName
  507.                             }
  508.                         }
  509.                     })
  510.                     Return errorResult
  511.                 End If
  512.  
  513.                 DirectoryCopy(oldPath, newPath)
  514.                 Dim result = New With {Key
  515.                     .Data = New With {Key
  516.                         .Id = newDirectoryPath, Key
  517.                         .Type = "folder", Key
  518.                         .Attributes = New With {Key
  519.                             .Name = directoryName, Key
  520.                             .Readable = 1, Key
  521.                             .Writable = 1, Key
  522.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  523.                             .Modified = GetUnixTimestamp(DateTime.Now)
  524.                         }
  525.                     }
  526.                 }
  527.                 Return result
  528.             Else
  529.                 Dim fileName = Path.GetFileName(source)
  530.                 Dim newFilePath = Path.Combine(target, fileName)
  531.                 Dim oldPath = Path.Combine(_webRootPath, source)
  532.                 Dim newPath = Path.Combine(_webRootPath, target, fileName)
  533.                 Dim fileExist = System.IO.File.Exists(newPath)
  534.  
  535.                 If fileExist Then
  536.                     Dim errorResult = New With {Key
  537.                         .Errors = New List(Of dynamic)()
  538.                     }
  539.                     errorResult.Errors.Add(New With {Key
  540.                         .Code = "500", Key
  541.                         .Title = "FILE_ALREADY_EXISTS", Key
  542.                         .Meta = New With {Key
  543.                             .Arguments = New List(Of String) From {
  544.                                 fileName
  545.                             }
  546.                         }
  547.                     })
  548.                     Return errorResult
  549.                 End If
  550.  
  551.                 System.IO.File.Copy(oldPath, newPath)
  552.                 Dim result = New With {Key
  553.                     .Data = New With {Key
  554.                         .Id = newFilePath, Key
  555.                         .Type = "file", Key
  556.                         .Attributes = New With {Key
  557.                             .Name = fileName, Key
  558.                             .Extension = Path.GetExtension(fileName).Replace(".", ""), Key
  559.                             .Readable = 1, Key
  560.                             .Writable = 1, Key
  561.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  562.                             .Modified = GetUnixTimestamp(DateTime.Now)
  563.                         }
  564.                     }
  565.                 }
  566.                 Return result
  567.             End If
  568.         End Function
  569.  
  570.         Private Function SaveFile(ByVal path As String, ByVal content As String) As dynamic
  571.             Dim filePath = Path.Combine(_webRootPath, path)
  572.             System.IO.File.WriteAllText(filePath, content)
  573.             Dim fileName = Path.GetFileName(path)
  574.             Dim fileExtension = Path.GetExtension(fileName)
  575.             Dim result = New With {Key
  576.                 .Data = New With {Key
  577.                     .Id = path, Key
  578.                     .Type = "file", Key
  579.                     .Attributes = New With {Key
  580.                         .Name = fileName, Key
  581.                         .Extension = fileExtension, Key
  582.                         .Readable = 1, Key
  583.                         .Writable = 1
  584.                     }
  585.                 }
  586.             }
  587.             Return result
  588.         End Function
  589.  
  590.         Private Function Delete(ByVal path As String) As dynamic
  591.             Dim fileAttributes = System.IO.File.GetAttributes(Path.Combine(_webRootPath, path))
  592.  
  593.             If (fileAttributes And FileAttributes.Directory) = FileAttributes.Directory Then
  594.                 Dim directoryName = Path.GetDirectoryName(path).Split("\"c).Last()
  595.                 Directory.Delete(Path.Combine(_webRootPath, path), True)
  596.                 Dim result = New With {Key
  597.                     .Data = New With {Key
  598.                         .Id = path, Key
  599.                         .Type = "folder", Key
  600.                         .Attributes = New With {Key
  601.                             .Name = directoryName, Key
  602.                             .Readable = 1, Key
  603.                             .Writable = 1, Key
  604.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  605.                             .Modified = GetUnixTimestamp(DateTime.Now), Key
  606.                             .Path = path
  607.                         }
  608.                     }
  609.                 }
  610.                 Return result
  611.             Else
  612.                 Dim fileName = Path.GetFileName(Path.Combine(_webRootPath, path))
  613.                 Dim fileExtension = Path.GetExtension(fileName).Replace(".", "")
  614.                 System.IO.File.Delete(Path.Combine(_webRootPath, path))
  615.                 Dim result = New With {Key
  616.                     .Data = New With {Key
  617.                         .Id = path, Key
  618.                         .Type = "file", Key
  619.                         .Attributes = New With {Key
  620.                             .Name = fileName, Key
  621.                             .Extension = fileExtension, Key
  622.                             .Readable = 1, Key
  623.                             .Writable = 1, Key
  624.                             .Created = GetUnixTimestamp(DateTime.Now), Key
  625.                             .Modified = GetUnixTimestamp(DateTime.Now)
  626.                         }
  627.                     }
  628.                 }
  629.                 Return result
  630.             End If
  631.         End Function
  632.  
  633.         Private Function ReadFile(ByVal path As String) As dynamic
  634.             Dim filePath = Path.Combine(_webRootPath, path)
  635.             Dim fileName = Path.GetFileName(filePath)
  636.             Dim fileBytes As Byte() = System.IO.File.ReadAllBytes(filePath)
  637.             Dim cd = New ContentDisposition With {
  638.                 .Inline = True,
  639.                 .FileName = fileName
  640.             }
  641.             Response.Headers.Add("Content-Disposition", cd.ToString())
  642.             Return File(fileBytes, "application/octet-stream")
  643.         End Function
  644.  
  645.         Private Function GetImage(ByVal path As String, ByVal thumbnail As Boolean) As IActionResult
  646.             Dim filePath = Path.Combine(_webRootPath, path)
  647.             Dim fileName = Path.GetFileName(filePath)
  648.             Dim fileBytes As Byte() = System.IO.File.ReadAllBytes(filePath)
  649.             Dim cd = New ContentDisposition With {
  650.                 .Inline = True,
  651.                 .FileName = fileName
  652.             }
  653.             Response.Headers.Add("Content-Disposition", cd.ToString())
  654.             Return File(fileBytes, "image/*")
  655.         End Function
  656.  
  657.         Private Function Download(ByVal path As String) As dynamic
  658.             Dim filePath = Path.Combine(_webRootPath, path)
  659.             Dim fileName = Path.GetFileName(filePath)
  660.             Dim fileBytes As Byte() = System.IO.File.ReadAllBytes(filePath)
  661.             Return File(fileBytes, "application/x-msdownload", fileName)
  662.         End Function
  663.  
  664.         Private Function Summarize() As dynamic
  665.             Dim directories = Directory.GetDirectories(_webRootPath, "*", SearchOption.AllDirectories).Length
  666.             Dim directoryInfo = New DirectoryInfo(_webRootPath)
  667.             Dim files = directoryInfo.GetFiles("*", SearchOption.AllDirectories)
  668.             Dim allSize = files.[Select](Function(f) f.Length).Sum()
  669.             Dim result = New With {Key
  670.                 .Data = New With {Key
  671.                     .Id = "/", Key
  672.                     .Type = "summary", Key
  673.                     .Attributes = New With {Key
  674.                         .Size = allSize, Key
  675.                         .Files = files.Length, Key
  676.                         .Folders = directories, Key
  677.                         .SizeLimit = 0
  678.                     }
  679.                 }
  680.             }
  681.             Return result
  682.         End Function
  683.  
  684.         Private Shared Sub DirectoryCopy(ByVal sourceDirName As String, ByVal destDirName As String)
  685.             Dim dir = New DirectoryInfo(sourceDirName)
  686.  
  687.             If Not dir.Exists Then
  688.                 Throw New DirectoryNotFoundException("Source directory does not exist or could not be found: " & sourceDirName)
  689.             End If
  690.  
  691.             Dim dirs = dir.GetDirectories()
  692.  
  693.             If Not Directory.Exists(destDirName) Then
  694.                 Directory.CreateDirectory(destDirName)
  695.             End If
  696.  
  697.             Dim files = dir.GetFiles()
  698.  
  699.             For Each file In files
  700.                 Dim temppath = Path.Combine(destDirName, file.Name)
  701.                 file.CopyTo(temppath, False)
  702.             Next
  703.  
  704.             For Each subdir In dirs
  705.                 Dim temppath = Path.Combine(destDirName, subdir.Name)
  706.                 DirectoryCopy(subdir.FullName, temppath)
  707.             Next
  708.         End Sub
  709.  
  710.         Private Shared Function MakeWebPath(ByVal path As String, ByVal Optional addSeperatorToBegin As Boolean = False, ByVal Optional addSeperatorToLast As Boolean = False) As String
  711.             path = path.Replace("\", "/")
  712.             If addSeperatorToBegin Then path = "/" & path
  713.             If addSeperatorToLast Then path = path & "/"
  714.             Return path
  715.         End Function
  716.     End Class
  717. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement