/// <inheritdoc /> public MappedResourceInfo?MapUrlPath(string urlPath, IMimeTypeProvider mimeTypeProvider) { if (urlPath.Length == 1) { return(null); } urlPath = Uri.UnescapeDataString(urlPath); var entry = _zipArchive.GetEntry(urlPath.Substring(1)); if (entry == null) { return(null); } return(MappedResourceInfo.ForFile( entry.FullName, entry.Name, entry.LastWriteTime.DateTime, entry.Length, mimeTypeProvider.GetMimeType(Path.GetExtension(entry.Name)))); }
public MediaPart ImportMedia(string relativePath, string filename, string contentType) { var mimeType = _mimeTypeProvider.GetMimeType(filename); if (!_storageProvider.FileExists(_storageProvider.Combine(relativePath, filename))) { return(null); } var storageFile = _storageProvider.GetFile(_storageProvider.Combine(relativePath, filename)); var mediaFile = BuildMediaFile(relativePath, storageFile); using (var stream = storageFile.OpenRead()) { var mediaFactory = GetMediaFactory(stream, mimeType, contentType); var mediaPart = mediaFactory.CreateMedia(stream, mediaFile.Name, mimeType, contentType); if (mediaPart != null) { mediaPart.FolderPath = relativePath; mediaPart.FileName = filename; } return(mediaPart); } }
public ActionResult Replace(int replaceId, string type, string url) { if (!Services.Authorizer.Authorize(Permissions.ManageOwnMedia)) { return(new HttpUnauthorizedResult()); } var replaceMedia = Services.ContentManager.Get <MediaPart>(replaceId); if (replaceMedia == null) { return(HttpNotFound()); } // Check permission if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent) && !_mediaLibraryService.CanManageMediaFolder(replaceMedia.FolderPath)) { return(new HttpUnauthorizedResult()); } var settings = Services.WorkContext.CurrentSite.As <MediaLibrarySettingsPart>(); var allowedExtensions = (settings.UploadAllowedFileTypeWhitelist ?? "") .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Where(x => x.StartsWith(".")); try { var filename = Path.GetFileName(url); // skip file if the allowed extensions is defined and doesn't match if (allowedExtensions.Any()) { if (!allowedExtensions.Any(e => filename.EndsWith(e, StringComparison.OrdinalIgnoreCase))) { throw new Exception(T("This file type is not allowed: {0}", Path.GetExtension(filename)).Text); } } var buffer = new WebClient().DownloadData(url); var stream = new MemoryStream(buffer); var mimeType = _mimeTypeProvider.GetMimeType(filename); string replaceContentType = _mediaLibraryService.MimeTypeToContentType(stream, mimeType, type) ?? type; if (!replaceContentType.Equals(replaceMedia.TypeDefinition.Name, StringComparison.OrdinalIgnoreCase)) { throw new Exception(T("Cannot replace {0} with {1}", replaceMedia.TypeDefinition.Name, replaceContentType).Text); } _mediaLibraryService.DeleteFile(replaceMedia.FolderPath, replaceMedia.FileName); _mediaLibraryService.UploadMediaFile(replaceMedia.FolderPath, replaceMedia.FileName, stream); replaceMedia.MimeType = mimeType; // Force a publish event which will update relevant Media properties replaceMedia.ContentItem.VersionRecord.Published = false; Services.ContentManager.Publish(replaceMedia.ContentItem); return(new JsonResult { Data = new { replaceMedia.FolderPath, MediaPath = replaceMedia.FileName } }); } catch (Exception e) { return(new JsonResult { Data = new { Success = false, error = e.Message } }); } }
public ActionResult Replace(int replaceId, string type) { if (!Services.Authorizer.Authorize(om.Permissions.ManageOwnMedia)) { return(new HttpUnauthorizedResult()); } var replaceMedia = Services.ContentManager.Get <MediaPart>(replaceId); if (replaceMedia == null) { return(HttpNotFound()); } // Check permission if (!(_mediaLibraryService.CheckMediaFolderPermission(om.Permissions.EditMediaContent, replaceMedia.FolderPath) && _mediaLibraryService.CheckMediaFolderPermission(om.Permissions.ImportMediaContent, replaceMedia.FolderPath)) && !_mediaLibraryService.CanManageMediaFolder(replaceMedia.FolderPath)) { return(new HttpUnauthorizedResult()); } var statuses = new List <object>(); var settings = Services.WorkContext.CurrentSite.As <MediaLibrarySettingsPart>(); // Loop through each file in the request for (int i = 0; i < HttpContext.Request.Files.Count; i++) { // Pointer to file var file = HttpContext.Request.Files[i]; var filename = Path.GetFileName(file.FileName); // if the file has been pasted, provide a default name if (file.ContentType.Equals("image/png", StringComparison.InvariantCultureIgnoreCase) && !filename.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) { filename = "clipboard.png"; } // skip file if the allowed extensions is defined and doesn't match if (!settings.IsFileAllowed(filename)) { statuses.Add(new { error = T("This file is not allowed: {0}", filename).Text, progress = 1.0, }); continue; } try { var mimeType = _mimeTypeProvider.GetMimeType(filename); string replaceContentType = _mediaLibraryService.MimeTypeToContentType(file.InputStream, mimeType, type) ?? type; if (!replaceContentType.Equals(replaceMedia.TypeDefinition.Name, StringComparison.OrdinalIgnoreCase)) { throw new Exception(T("Cannot replace {0} with {1}", replaceMedia.TypeDefinition.Name, replaceContentType).Text); } var mediaItemsUsingTheFile = Services.ContentManager.Query <MediaPart, MediaPartRecord>() .ForVersion(VersionOptions.Latest) .Where(x => x.FolderPath == replaceMedia.FolderPath && x.FileName == replaceMedia.FileName) .Count(); if (mediaItemsUsingTheFile == 1) { // if the file is referenced only by the deleted media content, the file too can be removed. _mediaLibraryService.DeleteFile(replaceMedia.FolderPath, replaceMedia.FileName); } else { // it changes the media file name replaceMedia.FileName = filename; } _mediaLibraryService.UploadMediaFile(replaceMedia.FolderPath, replaceMedia.FileName, file.InputStream); replaceMedia.MimeType = mimeType; // Force a publish event which will update relevant Media properties replaceMedia.ContentItem.VersionRecord.Published = false; Services.ContentManager.Publish(replaceMedia.ContentItem); statuses.Add(new { id = replaceMedia.Id, name = replaceMedia.Title, type = replaceMedia.MimeType, size = file.ContentLength, progress = 1.0, url = replaceMedia.FileName, }); } catch (Exception ex) { Logger.Error(ex, "Unexpected exception when uploading a media."); statuses.Add(new { error = T(ex.Message).Text, progress = 1.0, }); } } return(Json(statuses, JsonRequestBehavior.AllowGet)); }
public string GetMimeType(string fileName) { return(basic.GetMimeType(fileName) ?? registry.GetMimeType(fileName) ?? "text/html"); }
public ActionResult Replace(int replaceId, string type) { if (!Services.Authorizer.Authorize(Permissions.ManageOwnMedia)) { return(new HttpUnauthorizedResult()); } var replaceMedia = Services.ContentManager.Get <MediaPart>(replaceId); if (replaceMedia == null) { return(HttpNotFound()); } // Check permission if (!Services.Authorizer.Authorize(Permissions.ManageMediaContent) && !_mediaLibraryService.CanManageMediaFolder(replaceMedia.FolderPath)) { return(new HttpUnauthorizedResult()); } var statuses = new List <object>(); var settings = Services.WorkContext.CurrentSite.As <MediaLibrarySettingsPart>(); var allowedExtensions = (settings.UploadAllowedFileTypeWhitelist ?? "") .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Where(x => x.StartsWith(".")); // Loop through each file in the request for (int i = 0; i < HttpContext.Request.Files.Count; i++) { // Pointer to file var file = HttpContext.Request.Files[i]; var filename = Path.GetFileName(file.FileName); // if the file has been pasted, provide a default name if (file.ContentType.Equals("image/png", StringComparison.InvariantCultureIgnoreCase) && !filename.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) { filename = "clipboard.png"; } // skip file if the allowed extensions is defined and doesn't match if (allowedExtensions.Any()) { if (!allowedExtensions.Any(e => filename.EndsWith(e, StringComparison.OrdinalIgnoreCase))) { statuses.Add(new { error = T("This file type is not allowed: {0}", Path.GetExtension(filename)).Text, progress = 1.0, }); continue; } } try { var mimeType = _mimeTypeProvider.GetMimeType(filename); string replaceContentType = _mediaLibraryService.MimeTypeToContentType(file.InputStream, mimeType, type) ?? type; if (!replaceContentType.Equals(replaceMedia.TypeDefinition.Name, StringComparison.OrdinalIgnoreCase)) { throw new Exception(T("Cannot replace {0} with {1}", replaceMedia.TypeDefinition.Name, replaceContentType).Text); } _mediaLibraryService.DeleteFile(replaceMedia.FolderPath, replaceMedia.FileName); _mediaLibraryService.UploadMediaFile(replaceMedia.FolderPath, replaceMedia.FileName, file.InputStream); replaceMedia.MimeType = mimeType; // Force a publish event which will update relevant Media properties replaceMedia.ContentItem.VersionRecord.Published = false; Services.ContentManager.Publish(replaceMedia.ContentItem); statuses.Add(new { id = replaceMedia.Id, name = replaceMedia.Title, type = replaceMedia.MimeType, size = file.ContentLength, progress = 1.0, url = replaceMedia.FileName, }); } catch (Exception ex) { Logger.Error(ex, "Unexpected exception when uploading a media."); statuses.Add(new { error = T(ex.Message).Text, progress = 1.0, }); } } return(Json(statuses, JsonRequestBehavior.AllowGet)); }