Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to overwrite the media #17113

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/OrchardCore.Cms.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
// "AllowedFileExtensions": [".jpg",".jpeg",".png",".gif",".ico",".svg",".webp",".pdf",".doc",".docx",".ppt",".pptx",".pps",".ppsx",".odt",".xls",".xlsx",".psd",".mp3",".m4a",".ogg",".wav",".mp4",".m4v",".mov",".wmv",".avi",".mpg",".ogv",".3gp",".webm"],
// "ContentSecurityPolicy": "default-src 'self'; style-src 'unsafe-inline'",
// "MaxUploadChunkSize": 104857600,
// "TemporaryFileLifetime": "01:00:00"
// "TemporaryFileLifetime": "01:00:00",
// "OverwriteMedia": false
//},
// See https://docs.orchardcore.net/en/latest/reference/modules/Media.AmazonS3/#configuration to configure media storage in Amazon S3 Storage.
//"OrchardCore_Media_AmazonS3": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class MetaWeblogHandler : IXmlRpcHandler
private readonly IMembershipService _membershipService;
private readonly IEnumerable<IMetaWeblogDriver> _metaWeblogDrivers;
private readonly ISession _session;
private readonly MediaOptions _mediaOptions;

protected readonly IStringLocalizer S;

public MetaWeblogHandler(
Expand All @@ -42,6 +45,7 @@ public MetaWeblogHandler(
IContentDefinitionManager contentDefinitionManager,
IMediaFileStore mediaFileStore,
IEnumerable<IMetaWeblogDriver> metaWeblogDrivers,
IOptions<MediaOptions> mediaOptions,
IStringLocalizer<MetaWeblogHandler> localizer)
{
_contentManager = contentManager;
Expand All @@ -51,6 +55,7 @@ public MetaWeblogHandler(
_session = session;
_mediaFileStore = mediaFileStore;
_membershipService = membershipService;
_mediaOptions = mediaOptions.Value;
S = localizer;
}

Expand Down Expand Up @@ -157,7 +162,7 @@ private async Task<XRpcStruct> MetaWeblogNewMediaObjectAsync(string userName, st
try
{
stream = new MemoryStream(bits);
filePath = await _mediaFileStore.CreateFileFromStreamAsync(filePath, stream);
filePath = await _mediaFileStore.CreateFileFromStreamAsync(filePath, stream, _mediaOptions.OverwriteMedia);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public async Task<IActionResult> Upload(string path, string extensions)
{
var mediaFilePath = _mediaFileStore.Combine(path, fileName);
stream = file.OpenReadStream();
mediaFilePath = await _mediaFileStore.CreateFileFromStreamAsync(mediaFilePath, stream);
mediaFilePath = await _mediaFileStore.CreateFileFromStreamAsync(mediaFilePath, stream, _mediaOptions.OverwriteMedia);

var mediaFile = await _mediaFileStore.GetFileInfoAsync(mediaFilePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void Configure(MediaOptions options)
options.UseTokenizedQueryString = section.GetValue("UseTokenizedQueryString", DefaultUseTokenizedQueryString);
options.MaxUploadChunkSize = section.GetValue(nameof(options.MaxUploadChunkSize), DefaultMaxUploadChunkSize);
options.TemporaryFileLifetime = section.GetValue(nameof(options.TemporaryFileLifetime), _defaultTemporaryFileLifeTime);
options.OverwriteMedia = section.GetValue("OverwriteMedia", false);

var contentSecurityPolicy = section.GetValue("ContentSecurityPolicy", DefaultContentSecurityPolicy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace OrchardCore.Media;

public class MediaOptions
{
public const string EncryptedCommandCacheKeyPrefix = "MediaCommands:";

/// <summary>
/// The accepted sizes for custom width and height.
/// When <see cref="UseTokenizedQueryString"/> is enabled all sizes are valid
Expand Down Expand Up @@ -94,5 +96,8 @@ public class MediaOptions
/// </summary>
public TimeSpan TemporaryFileLifetime { get; set; }

public const string EncryptedCommandCacheKeyPrefix = "MediaCommands:";
/// <summary>
/// Gets or sets whether media files should be overwritten when uploading a file with the same name. Defaults to <see langword="false"/>.
/// </summary>
public bool OverwriteMedia { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.FileStorage.AzureBlob;
using OrchardCore.Media;
using OrchardCore.Modules;
using OrchardCore.Shells.Azure.Configuration;
using OrchardCore.Shells.Azure.Services;
Expand Down Expand Up @@ -33,10 +34,11 @@ public static OrchardCoreBuilder AddAzureShellsConfiguration(this OrchardCoreBui

var clock = sp.GetRequiredService<IClock>();
var contentTypeProvider = sp.GetRequiredService<IContentTypeProvider>();
var mediaOptions = sp.GetService<IOptions<MediaOptions>>().Value;

var fileStore = new BlobFileStore(blobOptions, clock, contentTypeProvider);

return new BlobShellsFileStore(fileStore);
return new BlobShellsFileStore(fileStore, mediaOptions);
});

services.Replace(ServiceDescriptor.Singleton<IShellsSettingsSources>(sp =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\OrchardCore.Abstractions\OrchardCore.Abstractions.csproj" />
<ProjectReference Include="..\OrchardCore.FileStorage.AzureBlob\OrchardCore.FileStorage.AzureBlob.csproj" />
<ProjectReference Include="..\OrchardCore.Media.Abstractions\OrchardCore.Media.Abstractions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using OrchardCore.FileStorage;
using OrchardCore.Media;

namespace OrchardCore.Shells.Azure.Services;

public class BlobShellsFileStore : IShellsFileStore
{
private readonly IFileStore _fileStore;
private readonly MediaOptions _mediaOptions;

public BlobShellsFileStore(IFileStore fileStore)
public BlobShellsFileStore(IFileStore fileStore, MediaOptions mediaOptions)
{
_fileStore = fileStore;
_mediaOptions = mediaOptions;
}

public Task<string> CreateFileFromStreamAsync(string path, Stream inputStream)
=> _fileStore.CreateFileFromStreamAsync(path, inputStream, true);
=> _fileStore.CreateFileFromStreamAsync(path, inputStream, _mediaOptions.OverwriteMedia);

public Task<IFileStoreEntry> GetFileInfoAsync(string path)
=> _fileStore.GetFileInfoAsync(path);
Expand Down
5 changes: 5 additions & 0 deletions src/docs/releases/3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ will return `false` for administrators, even though they still have full access.
{% assign isAuthorized = User | has_permission: "AccessAdminPanel" %}
```

### Media Module

An `OverwriteMedia` option has been added to `MediaOptions` to allow you to configure whether the media will be overwritten if it exists or not.


### Sealing Types

Many type commonly used by classes can be `sealed`, which improves runtime performance. While it's not mandatory, we recommend that you consider applying this improvement to your own extensions as well. We've implemented this enhancement in pull request [#16897](https://github.com/OrchardCMS/OrchardCore/pull/16897).
Expand Down
Loading