Goes brute force to try to resolve: U4-4049 Some images fail when uploading multiple media files
This commit is contained in:
@@ -1,10 +1,42 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace Umbraco.Core.IO
|
||||
{
|
||||
public static class FileSystemExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to open the file at <code>filePath</code> up to <code>maxRetries</code> times,
|
||||
/// with a thread sleep time of <code>sleepPerRetryInMilliseconds</code> between retries.
|
||||
/// </summary>
|
||||
public static FileStream OpenReadWithRetry(this FileInfo file, int maxRetries = 5, int sleepPerRetryInMilliseconds = 50)
|
||||
{
|
||||
var retries = maxRetries;
|
||||
|
||||
while (retries > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.OpenRead(file.FullName);
|
||||
}
|
||||
catch(IOException)
|
||||
{
|
||||
retries--;
|
||||
|
||||
if (retries == 0)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
Thread.Sleep(sleepPerRetryInMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentException("Retries must be greater than zero");
|
||||
}
|
||||
|
||||
public static long GetSize(this IFileSystem fs, string path)
|
||||
{
|
||||
using (var file = fs.OpenFile(path))
|
||||
|
||||
@@ -32,6 +32,7 @@ using umbraco;
|
||||
using umbraco.BusinessLogic.Actions;
|
||||
using Constants = Umbraco.Core.Constants;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Persistence.FaultHandling;
|
||||
|
||||
namespace Umbraco.Web.Editors
|
||||
{
|
||||
@@ -404,7 +405,11 @@ namespace Umbraco.Web.Editors
|
||||
|
||||
var mediaService = ApplicationContext.Services.MediaService;
|
||||
var f = mediaService.CreateMedia(fileName, parentId, mediaType);
|
||||
using (var fs = System.IO.File.OpenRead(file.LocalFileName))
|
||||
|
||||
var fileInfo = new FileInfo(file.LocalFileName);
|
||||
var fs = fileInfo.OpenReadWithRetry();
|
||||
if (fs == null) throw new InvalidOperationException("Could not acquire file stream");
|
||||
using (fs)
|
||||
{
|
||||
f.SetValue(Constants.Conventions.Media.File, fileName, fs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user