fix issue #134 - ensure files are not locked by another processs and remove loading indicator

This commit is contained in:
Shaun Walker 2019-10-17 11:39:15 -04:00
parent 82af078677
commit 73feb1f93f
3 changed files with 52 additions and 7 deletions

View File

@ -17,7 +17,7 @@ else
<th>&nbsp;</th>
</Header>
<Row>
<td>@context</td>
<td><a href="@(uri.Scheme + "://" + uri.Authority + "/" + PageState.Site.SiteRootPath + context)" target="_new">@context</a></td>
<td>
<button type="button" class="btn btn-danger" @onclick=@(async () => await DeleteFile(context))>Delete</button>
</td>
@ -29,10 +29,12 @@ else
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
List<string> Files;
Uri uri;
protected override async Task OnParametersSetAsync()
{
Files = await FileService.GetFilesAsync(PageState.Site.SiteRootPath);
uri = new Uri(NavigationManager.Uri);
}
private async Task DeleteFile(string filename)

View File

@ -56,6 +56,7 @@
public void AddModuleMessage(string message, MessageType type)
{
progressindicator = false;
StateHasChanged();
modulemessage.SetModuleMessage(message, type);
}

View File

@ -6,6 +6,7 @@ using Oqtane.Shared;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Oqtane.Controllers
@ -67,11 +68,11 @@ namespace Oqtane.Controllers
string[] fileparts = Directory.GetFiles(folder, filename + token + "*"); // list of all file parts
// if all of the file parts exist ( note that file parts can arrive out of order )
if (fileparts.Length == totalparts)
if (fileparts.Length == totalparts && CanAccessFiles(fileparts))
{
// merge file parts
bool success = true;
using (var stream = new FileStream(Path.Combine(folder, filename), FileMode.Create))
using (var stream = new FileStream(Path.Combine(folder, filename + ".tmp"), FileMode.Create))
{
foreach (string filepart in fileparts)
{
@ -89,7 +90,7 @@ namespace Oqtane.Controllers
}
}
// delete file parts
// delete file parts and rename file
if (success)
{
foreach (string filepart in fileparts)
@ -100,13 +101,17 @@ namespace Oqtane.Controllers
// check for allowable file extensions
if (!WhiteList.Contains(Path.GetExtension(filename).Replace(".", "")))
{
System.IO.File.Delete(Path.Combine(folder, filename));
success = false;
System.IO.File.Delete(Path.Combine(folder, filename + ".tmp"));
}
else
{
// rename file now that the entire process is completed
System.IO.File.Move(Path.Combine(folder, filename + ".tmp"), Path.Combine(folder, filename));
}
}
}
// clean up file parts which are more than 2 hours old ( which can happen if a file upload failed )
// clean up file parts which are more than 2 hours old ( which can happen if a prior file upload failed )
fileparts = Directory.GetFiles(folder, "*" + token + "*");
foreach (string filepart in fileparts)
{
@ -118,6 +123,43 @@ namespace Oqtane.Controllers
}
}
private bool CanAccessFiles(string[] files)
{
// ensure files are not locked by another process ( ie. still being written to )
bool canaccess = true;
FileStream stream = null;
foreach (string file in files)
{
int attempts = 0;
bool locked = true;
while (attempts < 5 && locked == true)
{
try
{
stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None);
locked = false;
}
catch // file is locked by another process
{
Thread.Sleep(1000); // wait 1 second
}
finally
{
if (stream != null)
{
stream.Close();
}
}
attempts += 1;
}
if (locked && canaccess)
{
canaccess = false;
}
}
return canaccess;
}
// DELETE api/<controller>/?folder=x&file=y
[HttpDelete]
[Authorize(Roles = Constants.AdminRole)]