Skip to content

Commit 14a8a3c

Browse files
committed
(mm) more work on MediaManager
1 parent 9f3474c commit 14a8a3c

35 files changed

Lines changed: 1950 additions & 676 deletions

src/Libraries/SmartStore.Core/IO/LocalFileSystem.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,15 @@ public void SaveStream(string path, Stream inputStream)
414414

415415
using (var outputStream = file.OpenWrite())
416416
{
417-
var buffer = new byte[8192];
418-
for (;;)
419-
{
420-
var length = inputStream.Read(buffer, 0, buffer.Length);
421-
if (length <= 0)
422-
break;
423-
outputStream.Write(buffer, 0, length);
424-
}
417+
inputStream.CopyTo(outputStream);
418+
//var buffer = new byte[8192];
419+
//for (;;)
420+
//{
421+
// var length = inputStream.Read(buffer, 0, buffer.Length);
422+
// if (length <= 0)
423+
// break;
424+
// outputStream.Write(buffer, 0, length);
425+
//}
425426
}
426427
}
427428

@@ -433,14 +434,15 @@ public async Task SaveStreamAsync(string path, Stream inputStream)
433434

434435
using (var outputStream = file.OpenWrite())
435436
{
436-
var buffer = new byte[8192];
437-
for (;;)
438-
{
439-
var length = await inputStream.ReadAsync(buffer, 0, buffer.Length);
440-
if (length <= 0)
441-
break;
442-
await outputStream.WriteAsync(buffer, 0, length);
443-
}
437+
await inputStream.CopyToAsync(outputStream);
438+
//var buffer = new byte[8192];
439+
//for (;;)
440+
//{
441+
// var length = await inputStream.ReadAsync(buffer, 0, buffer.Length);
442+
// if (length <= 0)
443+
// break;
444+
// await outputStream.WriteAsync(buffer, 0, length);
445+
//}
444446
}
445447
}
446448

@@ -482,7 +484,7 @@ internal static string ValidatePath(string basePath, string mappedPath)
482484
return mappedPath;
483485
}
484486

485-
private class LocalFile : IFile
487+
public class LocalFile : IFile
486488
{
487489
private readonly string _localPath;
488490
private readonly string _relativePath;
@@ -596,7 +598,7 @@ public Task<Stream> CreateFileAsync()
596598
}
597599
}
598600

599-
private class LocalFolder : IFolder
601+
public class LocalFolder : IFolder
600602
{
601603
private readonly string _localPath;
602604
private readonly string _relativePath;

src/Libraries/SmartStore.Core/Security/Permissions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ public static class CheckoutAttribute
499499
public static class Media
500500
{
501501
public const string Self = "media";
502+
public const string Update = "media.update";
503+
public const string Delete = "media.delete";
502504
public const string Upload = "media.upload";
503505

504506
public static class Download

src/Libraries/SmartStore.Services/Media/DefaultImageProcessor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public bool IsSupportedImage(string extension)
4343
.Any(x => x.Equals(extension, StringComparison.OrdinalIgnoreCase));
4444
}
4545

46-
public ProcessImageResult ProcessImage(ProcessImageQuery query)
46+
public ProcessImageResult ProcessImage(ProcessImageQuery query, bool disposeOutput = true)
4747
{
4848
Guard.NotNull(query, nameof(query));
4949

@@ -99,7 +99,8 @@ public ProcessImageResult ProcessImage(ProcessImageQuery query)
9999
Query = query,
100100
SourceWidth = processor.Image.Width,
101101
SourceHeight = processor.Image.Height,
102-
SourceMimeType = processor.CurrentImageFormat.MimeType
102+
SourceMimeType = processor.CurrentImageFormat.MimeType,
103+
DisposeOutputStream = disposeOutput
103104
};
104105

105106
// Core processing

src/Libraries/SmartStore.Services/Media/IImageProcessor.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ public interface IImageProcessor
1414
/// <returns>A value indicating whether processing is possible</returns>
1515
bool IsSupportedImage(string extension);
1616

17-
/// <summary>
18-
/// Processes an image
19-
/// </summary>
20-
/// <param name="request">Resize request</param>
21-
/// <returns>The resizing result encapsulated in <see cref="ProcessImageResult"/> type</returns>
22-
ProcessImageResult ProcessImage(ProcessImageQuery query);
17+
/// <summary>
18+
/// Processes an image
19+
/// </summary>
20+
/// <param name="request">Resize request</param>
21+
/// <param name="disposeOutput">Whether to dispose the output stream when <see cref="ProcessImageResult"/> instance gets disposed.</param>
22+
/// <returns>The resizing result encapsulated in <see cref="ProcessImageResult"/> type</returns>
23+
ProcessImageResult ProcessImage(ProcessImageQuery query, bool disposeOutput = true);
2324

2425
/// <summary>
2526
/// Gets the cumulative total processing time since app start in miliseconds

src/Libraries/SmartStore.Services/Media/ImageCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ protected string GetCachedImagePath4(int? mediaFileId, MediaPathData data, Proce
341341
// xxxxxxx
342342
if (mediaFileId.GetValueOrDefault() > 0)
343343
{
344-
result = mediaFileId.Value.ToString(IdFormatString).Grow(data.FileTitle, "-");
344+
result = mediaFileId.Value.ToString(IdFormatString);
345345
}
346346

347347
// xxxxxxx-f

src/Libraries/SmartStore.Services/Media/MediaHelper.cs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,9 @@
88
using SmartStore.Core.Data;
99
using SmartStore.Core.Domain.Media;
1010
using SmartStore.Core.Infrastructure;
11-
using SmartStore.Core.IO;
1211

1312
namespace SmartStore.Services.Media
1413
{
15-
public class MediaPathData
16-
{
17-
private string _title;
18-
private string _ext;
19-
private string _mime;
20-
21-
public MediaPathData(MediaFolderNode folder, string fileName)
22-
{
23-
Folder = folder;
24-
FileName = fileName;
25-
}
26-
27-
public MediaFolderNode Folder { get; }
28-
public string FileName { get; }
29-
30-
public string FileTitle
31-
{
32-
get
33-
{
34-
return _title ?? (_title = Path.GetFileNameWithoutExtension(FileName));
35-
}
36-
}
37-
38-
public string Extension
39-
{
40-
get
41-
{
42-
return _ext ?? (_ext = Path.GetExtension(FileName).EmptyNull());
43-
}
44-
set
45-
{
46-
_ext = value;
47-
}
48-
}
49-
50-
public string MimeType
51-
{
52-
get
53-
{
54-
return _mime ?? (_mime = MimeTypes.MapNameToMimeType(FileName));
55-
}
56-
set
57-
{
58-
_mime = value;
59-
}
60-
}
61-
}
62-
6314
public partial class MediaHelper
6415
{
6516
private readonly IFolderService _folderService;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using SmartStore.Core.IO;
4+
5+
namespace SmartStore.Services.Media
6+
{
7+
public class MediaPathData
8+
{
9+
private string _title;
10+
private string _ext;
11+
private string _mime;
12+
13+
public MediaPathData(MediaFolderNode folder, string fileName)
14+
{
15+
Folder = folder;
16+
FileName = fileName;
17+
}
18+
19+
public MediaPathData(MediaPathData pathData)
20+
{
21+
Folder = pathData.Folder;
22+
FileName = pathData.FileName;
23+
_title = pathData._title;
24+
_ext = pathData._ext;
25+
_mime = pathData._mime;
26+
}
27+
28+
public MediaFolderNode Folder { get; }
29+
public string FileName { get; }
30+
31+
public string FileTitle
32+
{
33+
get
34+
{
35+
return _title ?? (_title = Path.GetFileNameWithoutExtension(FileName));
36+
}
37+
}
38+
39+
public string Extension
40+
{
41+
get
42+
{
43+
return _ext ?? (_ext = Path.GetExtension(FileName).EmptyNull().TrimStart('.'));
44+
}
45+
set
46+
{
47+
_ext = value;
48+
}
49+
}
50+
51+
public string MimeType
52+
{
53+
get
54+
{
55+
return _mime ?? (_mime = MimeTypes.MapNameToMimeType(FileName));
56+
}
57+
set
58+
{
59+
_mime = value;
60+
}
61+
}
62+
}
63+
64+
}

src/Libraries/SmartStore.Services/Media/ProcessImageQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static NameValueCollection SanitizeCollection(NameValueCollection query)
8888
public string FileName { get; set; }
8989

9090
/// <summary>
91-
/// Whether to dispose the source stream after resizing completes
91+
/// Whether to dispose the source stream after processing completes
9292
/// </summary>
9393
public bool DisposeSource { get; set; }
9494

src/Libraries/SmartStore.Services/Media/ProcessImageResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class ProcessImageResult : DisposableObject
88
public ProcessImageQuery Query { get; set; }
99

1010
public MemoryStream OutputStream { get; set; }
11+
internal bool DisposeOutputStream { get; set; }
1112

1213
public int? SourceWidth { get; set; }
1314
public int? SourceHeight { get; set; }
@@ -28,7 +29,7 @@ public class ProcessImageResult : DisposableObject
2829

2930
protected override void OnDispose(bool disposing)
3031
{
31-
if (disposing && OutputStream != null)
32+
if (disposing && DisposeOutputStream && OutputStream != null)
3233
{
3334
OutputStream.Dispose();
3435
OutputStream = null;

src/Libraries/SmartStore.Services/Media/Storage/DatabaseMediaStorageProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public static string SystemName
3131
get { return "MediaStorage.SmartStoreDatabase"; }
3232
}
3333

34+
public bool IsCloudStorage { get; } = false;
35+
36+
public string GetPublicUrl(MediaFile mediaFile)
37+
{
38+
return null;
39+
}
40+
3441
public long GetSize(MediaFile mediaFile)
3542
{
3643
Guard.NotNull(mediaFile, nameof(mediaFile));

0 commit comments

Comments
 (0)