mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-27 12:29:04 +08:00
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:
committed by
GitHub
parent
343ceb777b
commit
ddc56c8baa
@@ -171,6 +171,8 @@ namespace Orchard.MediaProcessing.Services {
|
||||
}
|
||||
}
|
||||
}
|
||||
// the storage provider may have altered the filepath
|
||||
filterContext.FilePath = newFile.GetPath();
|
||||
}
|
||||
}
|
||||
catch(Exception e) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user