// This action will demonstrate how to create a file in the Media folder and read it from there. public async Task <string> CreateFileInMediaFolder() { // You need to initialize a stream if you have a specific text you want to write into the file. If you // already have a stream for that just use it (you'll see it later)! using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hi there!"))) { // The third parameter here is optional - if true, it will override the file if already exists. await _mediaFileStore.CreateFileFromStream(TestFileRelativePath, stream, true); } // Use this method to check if the file exists (it will be null if the file doesn't exist). It's similar to // the built-in FileInfo class but not that robust. var fileInfo = await _mediaFileStore.GetFileInfoAsync(TestFileRelativePath); // The IMediaFileStore has its own specific methods such as mapping the file path to a public URL. Since // the files in the Media folder are accessible from the outside this can be handy. var publicUrl = _mediaFileStore.MapPathToPublicUrl(TestFileRelativePath); return($"Successfully created file! File size: {fileInfo.Length} bytes. Public URL: {publicUrl}"); }
public async Task <ActionResult> Upload( string path, string contentType, ICollection <IFormFile> files) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageOwnMedia)) { return(Unauthorized()); } if (string.IsNullOrEmpty(path)) { path = ""; } var result = new List <object>(); // TODO: Validate file extensions // Loop through each file in the request foreach (var file in files) { // TODO: support clipboard try { var mediaFilePath = _mediaFileStore.Combine(path, file.FileName); using (var stream = file.OpenReadStream()) { await _mediaFileStore.CreateFileFromStream(mediaFilePath, stream); } var mediaFile = await _mediaFileStore.GetFileInfoAsync(mediaFilePath); result.Add(CreateFileResult(mediaFile)); } catch (Exception ex) { _logger.LogError(ex, "An error occured while uploading a media"); result.Add(new { name = file.FileName, size = file.Length, folder = path, error = ex.Message }); } } return(Json(new { files = result.ToArray() })); }
public async Task ExecuteAsync(RecipeExecutionContext context) { if (!String.Equals(context.Name, "media", StringComparison.OrdinalIgnoreCase)) { return; } var model = context.Step.ToObject <MediaStepModel>(); foreach (JObject item in model.Files) { var file = item.ToObject <MediaStepFile>(); using (var stream = new MemoryStream(Convert.FromBase64String(file.Base64))) { await _mediaFileStore.CreateFileFromStream(file.Path, stream); } } }
private async Task <XRpcStruct> MetaWeblogNewMediaObjectAsync(string userName, string password, XRpcStruct file) { var user = await ValidateUserAsync(userName, password); var name = file.Optional <string>("name"); var bits = file.Optional <byte[]>("bits"); string directoryName = Path.GetDirectoryName(name); string filePath = _mediaFileStore.Combine(directoryName, Path.GetFileName(name)); await _mediaFileStore.CreateFileFromStream(filePath, new MemoryStream(bits)); string publicUrl = _mediaFileStore.MapPathToPublicUrl(filePath); return(new XRpcStruct() // Some clients require all optional attributes to be declared Wordpress responds in this way as well. .Set("file", publicUrl) .Set("url", publicUrl) .Set("type", file.Optional <string>("type"))); }
public async Task <ActionResult <object> > Upload( string path, string contentType, ICollection <IFormFile> files) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageOwnMedia)) { return(Unauthorized()); } if (string.IsNullOrEmpty(path)) { path = ""; } var section = _shellConfiguration.GetSection("OrchardCore.Media"); // var maxUploadSize = section.GetValue("MaxRequestBodySize", 100_000_000); var maxFileSize = section.GetValue("MaxFileSize", 30_000_000); var allowedFileExtensions = section.GetValue("AllowedFileExtensions", DefaultAllowedFileExtensions); var result = new List <object>(); // Loop through each file in the request foreach (var file in files) { // TODO: support clipboard if (!allowedFileExtensions.Contains(Path.GetExtension(file.FileName), StringComparer.OrdinalIgnoreCase)) { result.Add(new { name = file.FileName, size = file.Length, folder = path, error = T["This file extension is not allowed: {0}", Path.GetExtension(file.FileName)].ToString() }); _logger.LogInformation($"File extension not allowed: '{file.FileName}'"); continue; } if (file.Length > maxFileSize) { result.Add(new { name = file.FileName, size = file.Length, folder = path, error = T["The file {0} is too big. The limit is {1}MB", file.FileName, (int)Math.Floor((double)maxFileSize / 1024 / 1024)].ToString() }); _logger.LogInformation($"File too big: '{file.FileName}' ({file.Length}B)"); continue; } try { var mediaFilePath = _mediaFileStore.Combine(path, file.FileName); using (var stream = file.OpenReadStream()) { await _mediaFileStore.CreateFileFromStream(mediaFilePath, stream); } var mediaFile = await _mediaFileStore.GetFileInfoAsync(mediaFilePath); result.Add(CreateFileResult(mediaFile)); } catch (Exception ex) { _logger.LogError(ex, "An error occured while uploading a media"); result.Add(new { name = file.FileName, size = file.Length, folder = path, error = ex.Message }); } } return(new { files = result.ToArray() }); }