Skip to content

Commit

Permalink
#5894 Fixed RoxyFileman issue with Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
skoshelev committed Oct 12, 2021
1 parent fc90521 commit 30fd49c
Showing 1 changed file with 47 additions and 29 deletions.
Expand Up @@ -45,6 +45,15 @@ public class FileRoxyFilemanService : BaseRoxyFilemanService, IRoxyFilemanServic

#region Utilities

protected virtual HttpResponse GetJsonResponse()
{
var response = GetHttpContext().Response;

response.Headers.TryAdd("Content-Type", "application/json");

return response;
}

/// <summary>
/// Сopy the directory with the embedded files and directories
/// </summary>
Expand Down Expand Up @@ -229,7 +238,7 @@ public virtual async Task CopyDirectoryAsync(string sourcePath, string destinati

await BaseCopyDirectoryAsync(directoryPath, newDirectoryPath);

await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}

/// <summary>
Expand All @@ -252,7 +261,7 @@ public virtual async Task CreateDirectoryAsync(string parentDirectoryPath, strin
var path = _fileProvider.Combine(parentDirectoryPath, name);
_fileProvider.CreateDirectory(path);

await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -284,7 +293,7 @@ public virtual async Task DeleteDirectoryAsync(string path)
try
{
_fileProvider.DeleteDirectory(path);
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -318,10 +327,12 @@ public async Task DownloadDirectoryAsync(string path)

ZipFile.CreateFromDirectory(fullPath, zipPath, CompressionLevel.Fastest, true);

GetHttpContext().Response.Clear();
GetHttpContext().Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{WebUtility.UrlEncode(zipName)}\"");
GetHttpContext().Response.ContentType = MimeTypes.ApplicationForceDownload;
await GetHttpContext().Response.SendFileAsync(zipPath);
var response = GetHttpContext().Response;

response.Clear();
response.Headers.Add("Content-Disposition", $"attachment; filename=\"{WebUtility.UrlEncode(zipName)}\"");
response.ContentType = MimeTypes.ApplicationForceDownload;
await response.SendFileAsync(zipPath);

_fileProvider.DeleteFile(zipPath);
}
Expand All @@ -342,16 +353,18 @@ public virtual async Task GetDirectoriesAsync(string type)
allDirectories.Insert(0, rootDirectoryPath);

var localPath = GetFullPath(null);
await GetHttpContext().Response.WriteAsync("[");
var response = GetJsonResponse();

await response.WriteAsync("[");
for (var i = 0; i < allDirectories.Count; i++)
{
var directoryPath = (string)allDirectories[i];
await GetHttpContext().Response.WriteAsync($"{{\"p\":\"/{directoryPath.Replace(localPath, string.Empty).Replace("\\", "/").TrimStart('/')}\",\"f\":\"{(await GetFilesByDirectoryAsync(directoryPath, type)).Count}\",\"d\":\"{_fileProvider.GetDirectories(directoryPath).Length}\"}}");
await response.WriteAsync($"{{\"p\":\"/{directoryPath.Replace(localPath, string.Empty).Replace("\\", "/").TrimStart('/')}\",\"f\":\"{(await GetFilesByDirectoryAsync(directoryPath, type)).Count}\",\"d\":\"{_fileProvider.GetDirectories(directoryPath).Length}\"}}");
if (i < allDirectories.Count - 1)
await GetHttpContext().Response.WriteAsync(",");
await response.WriteAsync(",");
}

await GetHttpContext().Response.WriteAsync("]");
await response.WriteAsync("]");
}

/// <summary>
Expand Down Expand Up @@ -381,7 +394,7 @@ public virtual async Task MoveDirectoryAsync(string sourcePath, string destinati
try
{
_fileProvider.DirectoryMove(fullSourcePath, destinationPath);
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -419,7 +432,7 @@ public virtual async Task RenameDirectoryAsync(string sourcePath, string newName
try
{
_fileProvider.DirectoryMove(fullSourcePath, destinationDirectory);
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -453,7 +466,7 @@ public virtual async Task CopyFileAsync(string sourcePath, string destinationPat
try
{
_fileProvider.FileCopy(filePath, _fileProvider.Combine(destinationPath, newFileName));
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand All @@ -478,7 +491,7 @@ public virtual async Task DeleteFileAsync(string path)
try
{
_fileProvider.DeleteFile(path);
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand All @@ -500,10 +513,12 @@ public virtual async Task DownloadFileAsync(string path)

if (_fileProvider.FileExists(filePath))
{
GetHttpContext().Response.Clear();
GetHttpContext().Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{WebUtility.UrlEncode(_fileProvider.GetFileName(filePath))}\"");
GetHttpContext().Response.ContentType = MimeTypes.ApplicationForceDownload;
await GetHttpContext().Response.SendFileAsync(filePath);
var response = GetHttpContext().Response;

response.Clear();
response.Headers.Add("Content-Disposition", $"attachment; filename=\"{WebUtility.UrlEncode(_fileProvider.GetFileName(filePath))}\"");
response.ContentType = MimeTypes.ApplicationForceDownload;
await response.SendFileAsync(filePath);
}
}

Expand All @@ -517,8 +532,9 @@ public virtual async Task GetFilesAsync(string directoryPath, string type)
{
directoryPath = await GetVirtualPathAsync(directoryPath);
var files = await GetFilesByDirectoryAsync(GetFullPath(directoryPath), type);
var response = GetJsonResponse();

await GetHttpContext().Response.WriteAsync("[");
await response.WriteAsync("[");
for (var i = 0; i < files.Count; i++)
{
var width = 0;
Expand All @@ -533,13 +549,13 @@ public virtual async Task GetFilesAsync(string directoryPath, string type)
height = image.Height;
}

await GetHttpContext().Response.WriteAsync($"{{\"p\":\"{directoryPath.TrimEnd('/')}/{_fileProvider.GetFileName(physicalPath)}\",\"t\":\"{Math.Ceiling(GetTimestamp(_fileProvider.GetLastWriteTime(physicalPath)))}\",\"s\":\"{_fileProvider.FileLength(physicalPath)}\",\"w\":\"{width}\",\"h\":\"{height}\"}}");
await response.WriteAsync($"{{\"p\":\"{directoryPath.TrimEnd('/')}/{_fileProvider.GetFileName(physicalPath)}\",\"t\":\"{Math.Ceiling(GetTimestamp(_fileProvider.GetLastWriteTime(physicalPath)))}\",\"s\":\"{_fileProvider.FileLength(physicalPath)}\",\"w\":\"{width}\",\"h\":\"{height}\"}}");

if (i < files.Count - 1)
await GetHttpContext().Response.WriteAsync(",");
await response.WriteAsync(",");
}

await GetHttpContext().Response.WriteAsync("]");
await response.WriteAsync("]");
}

/// <summary>
Expand Down Expand Up @@ -569,7 +585,7 @@ public virtual async Task MoveFileAsync(string sourcePath, string destinationPat
try
{
_fileProvider.FileMove(fullSourcePath, destinationPath);
await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -601,7 +617,7 @@ public virtual async Task RenameFileAsync(string sourcePath, string newName)
{
_fileProvider.FileMove(fullSourcePath, destinationPath);

await GetHttpContext().Response.WriteAsync(GetSuccessResponse());
await GetJsonResponse().WriteAsync(GetSuccessResponse());
}
catch
{
Expand Down Expand Up @@ -663,7 +679,7 @@ await using (var stream = new FileStream(destinationFile, FileMode.OpenOrCreate)
if (hasErrors)
result = GetErrorResponse(await GetLanguageResourceAsync("E_UploadNotAll"));

await GetHttpContext().Response.WriteAsync(result);
await GetJsonResponse().WriteAsync(result);
}
else
await GetHttpContext().Response.WriteAsync($"<script>parent.fileUploaded({result});</script>");
Expand Down Expand Up @@ -701,16 +717,18 @@ public virtual async Task CreateImageThumbnailAsync(string path)
height = image.Height * (targetSize / (float)image.Width);
}

GetHttpContext().Response.Headers.Add("Content-Type", MimeTypes.ImagePng);
var response = GetHttpContext().Response;

response.Headers.Add("Content-Type", MimeTypes.ImagePng);

using (var bitmap = image.Resize(new SKImageInfo((int)width, (int)height), SKFilterQuality.None))
{
using var cropImg = SKImage.FromBitmap(bitmap);
file = cropImg.Encode().ToArray();
}

await GetHttpContext().Response.Body.WriteAsync(file, 0, file.Length);
GetHttpContext().Response.Body.Close();
await response.Body.WriteAsync(file, 0, file.Length);
response.Body.Close();
}

/// <summary>
Expand Down

0 comments on commit 30fd49c

Please sign in to comment.