Parcourir la source

Fixed ftp download file. Prevented memory stream overload

Marcel Baumgartner il y a 2 ans
Parent
commit
06c280704c

+ 15 - 17
Moonlight/App/Helpers/Files/FtpFileAccess.cs

@@ -1,6 +1,7 @@
 using System.Net;
 using System.Net;
 using System.Text;
 using System.Text;
 using FluentFTP;
 using FluentFTP;
+using Moonlight.App.Exceptions;
 
 
 namespace Moonlight.App.Helpers.Files;
 namespace Moonlight.App.Helpers.Files;
 
 
@@ -78,10 +79,10 @@ public class FtpFileAccess : FileAccess
     {
     {
         await EnsureConnect();
         await EnsureConnect();
 
 
-        var s = new MemoryStream();
+        var s = new MemoryStream(8 * 1024 * 1204); //TODO: Add config option
         await Client.DownloadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name);
         await Client.DownloadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name);
         var data = s.ToArray();
         var data = s.ToArray();
-        s.Dispose();
+        await s.DisposeAsync();
         var str = Encoding.UTF8.GetString(data);
         var str = Encoding.UTF8.GetString(data);
         return str;
         return str;
     }
     }
@@ -90,11 +91,11 @@ public class FtpFileAccess : FileAccess
     {
     {
         await EnsureConnect();
         await EnsureConnect();
 
 
-        var s = new MemoryStream();
+        var s = new MemoryStream(8 * 1024 * 1204); //TODO: Add config option
         s.Write(Encoding.UTF8.GetBytes(content));
         s.Write(Encoding.UTF8.GetBytes(content));
         s.Position = 0;
         s.Position = 0;
         await Client.UploadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name, FtpRemoteExists.Overwrite);
         await Client.UploadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name, FtpRemoteExists.Overwrite);
-        s.Dispose();
+        await s.DisposeAsync();
     }
     }
 
 
     public override async Task Upload(string name, Stream dataStream, Action<int>? progressUpdated = null)
     public override async Task Upload(string name, Stream dataStream, Action<int>? progressUpdated = null)
@@ -103,10 +104,9 @@ public class FtpFileAccess : FileAccess
 
 
         IProgress<FtpProgress> progress = new Progress<FtpProgress>(x =>
         IProgress<FtpProgress> progress = new Progress<FtpProgress>(x =>
         {
         {
-            progressUpdated((int) x.Progress);
+            progressUpdated?.Invoke((int)x.Progress);
         });
         });
         await Client.UploadStream(dataStream, CurrentPath.TrimEnd('/') + "/" + name, FtpRemoteExists.Overwrite, false, progress);
         await Client.UploadStream(dataStream, CurrentPath.TrimEnd('/') + "/" + name, FtpRemoteExists.Overwrite, false, progress);
-        dataStream.Dispose();
     }
     }
 
 
     public override async Task MkDir(string name)
     public override async Task MkDir(string name)
@@ -121,10 +121,8 @@ public class FtpFileAccess : FileAccess
         return Task.FromResult(CurrentPath);
         return Task.FromResult(CurrentPath);
     }
     }
 
 
-    public override async Task<string> DownloadUrl(FileData fileData)
+    public override Task<string> DownloadUrl(FileData fileData)
     {
     {
-        await EnsureConnect();
-
         throw new NotImplementedException();
         throw new NotImplementedException();
     }
     }
 
 
@@ -132,8 +130,12 @@ public class FtpFileAccess : FileAccess
     {
     {
         await EnsureConnect();
         await EnsureConnect();
 
 
-        var s = new MemoryStream();
-        await Client.DownloadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name);
+        var s = new MemoryStream(8 * 1024 * 1204); //TODO: Add config option
+        var downloaded = await Client.DownloadStream(s, CurrentPath.TrimEnd('/') + "/" + fileData.Name);
+
+        if (!downloaded)
+            throw new DisplayException("Unable to download file");
+        
         return s;
         return s;
     }
     }
 
 
@@ -157,17 +159,13 @@ public class FtpFileAccess : FileAccess
             await Client.MoveDirectory(CurrentPath.TrimEnd('/') + "/" + fileData.Name, newPath);
             await Client.MoveDirectory(CurrentPath.TrimEnd('/') + "/" + fileData.Name, newPath);
     }
     }
 
 
-    public override async Task Compress(params FileData[] files)
+    public override Task Compress(params FileData[] files)
     {
     {
-        await EnsureConnect();
-
         throw new NotImplementedException();
         throw new NotImplementedException();
     }
     }
 
 
-    public override async Task Decompress(FileData fileData)
+    public override Task Decompress(FileData fileData)
     {
     {
-        await EnsureConnect();
-
         throw new NotImplementedException();
         throw new NotImplementedException();
     }
     }
 
 

+ 2 - 2
Moonlight/Shared/Components/FileManagerPartials/FileManager.razor

@@ -166,8 +166,8 @@ else
                     {
                     {
                         var stream = await Access.DownloadStream(x);
                         var stream = await Access.DownloadStream(x);
                         await ToastService.Info(SmartTranslateService.Translate("Starting download"));
                         await ToastService.Info(SmartTranslateService.Translate("Starting download"));
-                        await FileService.AddBuffer(stream);
-                        await FileService.DownloadBinaryBuffers(x.Name);
+                        stream.Position = 0;
+                        await FileService.DownloadFile(fileName: x.Name, stream: stream, contentType: "application/octet-stream");
                     }
                     }
                     catch (NotImplementedException)
                     catch (NotImplementedException)
                     {
                     {