Moved logic for max file path length in file storage provider (#8603)

Co-authored-by: matteo.piovanelli <matteo.piovanelli@laser-group.com>
This commit is contained in:
Andrea Piovanelli
2022-09-05 09:01:30 +02:00
committed by GitHub
parent 343ceb777b
commit ddc56c8baa
2 changed files with 30 additions and 0 deletions

View File

@@ -171,6 +171,8 @@ namespace Orchard.MediaProcessing.Services {
}
}
}
// the storage provider may have altered the filepath
filterContext.FilePath = newFile.GetPath();
}
}
catch(Exception e) {

View File

@@ -34,10 +34,27 @@ namespace Orchard.FileSystems.Media {
_publicPath = appPath + "Media/" + settings.Name + "/";
T = NullLocalizer.Instance;
MaxPathLength = 260;
}
public Localizer T { get; set; }
public int MaxPathLength {
get; set;
// The public setter allows injecting this from Sites.MyTenant.Config or Sites.config, by using
// an AutoFac component:
/*
<component instance-scope="per-lifetime-scope"
type="Orchard.FileSystems.Media.FileSystemStorageProvider, Orchard.Framework"
service="Orchard.FileSystems.Media.IStorageProvider">
<properties>
<property name="MaxPathLength" value="500" />
</properties>
</component>
*/
}
/// <summary>
/// Maps a relative path into the storage path.
/// </summary>
@@ -335,6 +352,17 @@ namespace Orchard.FileSystems.Media {
if (!Directory.Exists(dirName)) {
Directory.CreateDirectory(dirName);
}
//Path.GetFileNameWithoutExtension(fileInfo.Name)
// If absolute path is longer than the maximum path length (260 characters), file cannot be saved.
if (fileInfo.FullName.Length > MaxPathLength) {
var fileName = Path.GetFileNameWithoutExtension(fileInfo.Name);
var extension = fileInfo.Extension;
// try to generate a shorter path for the file
var nameHash = fileName.GetHashCode().ToString("x");
// Hopefully this new path is short enough now
path = Combine(Path.GetDirectoryName(path), nameHash + extension);
fileInfo = new FileInfo(MapStorage(path));
}
File.WriteAllBytes(fileInfo.FullName, new byte[0]);
return new FileSystemStorageFile(Fix(path), fileInfo);