Beispiel #1
0
        private ImageGalleryImage CreateImageFromMediaFile(MediaFile mediaFile, ImageGallerySettingsRecord imageGallerySettings)
        {
            if (imageGallerySettings == null)
            {
                throw new ArgumentNullException("imageGallerySettings");
            }

            var  imageSettings        = GetImageSettings(imageGallerySettings, mediaFile.Name);
            bool isValidThumbnailSize = imageGallerySettings.ThumbnailWidth > 0 &&
                                        imageGallerySettings.ThumbnailHeight > 0;
            Thumbnail thumbnail = null;

            if (isValidThumbnailSize)
            {
                thumbnail = _thumbnailService.GetThumbnail(_storageProvider.Combine(mediaFile.FolderName, mediaFile.Name),
                                                           imageGallerySettings.ThumbnailWidth,
                                                           imageGallerySettings.ThumbnailHeight,
                                                           imageGallerySettings.KeepAspectRatio,
                                                           imageGallerySettings.ExpandToFill);
            }

            return(new ImageGalleryImage
            {
                PublicUrl = _mediaService.GetMediaPublicUrl(mediaFile.FolderName, mediaFile.Name),
                Name = mediaFile.Name,
                Size = mediaFile.Size,
                User = mediaFile.User,
                LastUpdated = mediaFile.LastUpdated,
                Caption = imageSettings == null ? string.Empty : imageSettings.Caption,
                Thumbnail = thumbnail,
                Title = imageSettings == null ? null : imageSettings.Title,
                Position = imageSettings == null ? 0 : imageSettings.Position
            });
        }
Beispiel #2
0
        public MediaPartHandler(
            IMediaLibraryService mediaLibraryService,
            IRepository <MediaPartRecord> repository,
            IStorageProvider storageProvider)
        {
            _mediaLibraryService = mediaLibraryService;
            _storageProvider     = storageProvider;

            Filters.Add(StorageFilter.For(repository));
            OnRemoving <MediaPart>((context, part) => RemoveMedia(part));
            OnLoaded <MediaPart>((context, part) => {
                if (!String.IsNullOrEmpty(part.FileName))
                {
                    part._publicUrl.Loader(x => _mediaLibraryService.GetMediaPublicUrl(part.FolderPath, part.FileName));
                }
            });

            OnIndexing <MediaPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("media-folderpath", Normalize(part.FolderPath)).Store()
                                   .Add("media-filename", Normalize(part.FileName)).Store()
                                   .Add("media-mimetype", Normalize(part.MimeType)).Store()
                                   .Add("media-caption", part.Caption).Analyze()
                                   .Add("media-alternatetext", part.AlternateText).Analyze()
                                   );

            OnIndexing <ImagePart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("image-height", part.Height).Analyze().Store()
                                   .Add("image-width", part.Width).Analyze().Store()
                                   );

            OnIndexing <DocumentPart>((context, part) =>
                                      context.DocumentIndex
                                      .Add("document-length", part.Length).Analyze().Store()
                                      );

            OnIndexing <VideoPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("video-length", part.Length).Analyze().Store()
                                   );

            OnIndexing <AudioPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("audio-length", part.Length).Analyze().Store()
                                   );

            OnIndexing <OEmbedPart>((context, part) =>
                                    context.DocumentIndex
                                    .Add("oembed-source", part.Source).Analyze().Store()
                                    );
        }
Beispiel #3
0
        public MediaPartHandler(
            IMediaLibraryService mediaLibraryService,
            IRepository<MediaPartRecord> repository, 
            IStorageProvider storageProvider) {
            _mediaLibraryService = mediaLibraryService;
            _storageProvider = storageProvider;

            Filters.Add(StorageFilter.For(repository));
            OnRemoving<MediaPart>((context, part) => RemoveMedia(part));
            OnLoaded<MediaPart>((context, part) => {
                if (!String.IsNullOrEmpty(part.FileName)) {
                    part._publicUrl.Loader(x => _mediaLibraryService.GetMediaPublicUrl(part.FolderPath, part.FileName));
                }
            });

            OnIndexing<MediaPart>((context, part) => 
                context.DocumentIndex
                    .Add("media-folderpath", Normalize(part.FolderPath)).Store()
                    .Add("media-filename", Normalize(part.FileName)).Store()
                    .Add("media-mimetype", Normalize(part.MimeType)).Store()
                    .Add("media-caption", part.Caption).Analyze()
                    .Add("media-alternatetext", part.AlternateText).Analyze()
                );

            OnIndexing<ImagePart>((context, part) =>
                context.DocumentIndex
                    .Add("image-height", part.Height).Analyze().Store()
                    .Add("image-width", part.Width).Analyze().Store()
                );

            OnIndexing<DocumentPart>((context, part) =>
                context.DocumentIndex
                    .Add("document-length", part.Length).Analyze().Store()
                );

            OnIndexing<VideoPart>((context, part) =>
                context.DocumentIndex
                    .Add("video-length", part.Length).Analyze().Store()
                );

            OnIndexing<AudioPart>((context, part) =>
                context.DocumentIndex
                    .Add("audio-length", part.Length).Analyze().Store()
                );

            OnIndexing<OEmbedPart>((context, part) =>
                context.DocumentIndex
                    .Add("oembed-source", part.Source).Analyze().Store()
                );
        }
        public IEnumerable <string> GetFaviconSuggestions()
        {
            List <string> faviconSuggestions = null;
            var           rootMediaFolders   = _mediaService
                                               .GetMediaFolders(".")
                                               .Where(f => f.Name.Equals(FaviconMediaFolder, StringComparison.OrdinalIgnoreCase));

            if (rootMediaFolders.Any())
            {
                faviconSuggestions = new List <string>(
                    _mediaService.GetMediaFiles(FaviconMediaFolder)
                    .Select(f => _mediaService.GetMediaPublicUrl(FaviconMediaFolder, f.Name)));
            }
            return(faviconSuggestions);
        }
Beispiel #5
0
        public MediaPartHandler(
            IMediaLibraryService mediaLibraryService,
            IRepository<MediaPartRecord> repository, 
            IStorageProvider storageProvider) {
            _mediaLibraryService = mediaLibraryService;
            _storageProvider = storageProvider;

            Filters.Add(StorageFilter.For(repository));
            OnRemoving<MediaPart>((context, part) => RemoveMedia(part));
            OnLoading<MediaPart>((context, part) => {
                if (!String.IsNullOrEmpty(part.FileName)) {
                    part._publicUrl.Loader(x => _mediaLibraryService.GetMediaPublicUrl(part.FolderPath, part.FileName));
                }
            });
        }
Beispiel #6
0
        public MediaPartHandler(
            IMediaLibraryService mediaLibraryService,
            IRepository <MediaPartRecord> repository,
            IStorageProvider storageProvider)
        {
            _mediaLibraryService = mediaLibraryService;
            _storageProvider     = storageProvider;

            Filters.Add(StorageFilter.For(repository));
            OnRemoving <MediaPart>((context, part) => RemoveMedia(part));
            OnLoading <MediaPart>((context, part) => {
                if (!String.IsNullOrEmpty(part.FileName))
                {
                    part._publicUrl.Loader(x => _mediaLibraryService.GetMediaPublicUrl(part.FolderPath, part.FileName));
                }
            });
        }
Beispiel #7
0
        public ActionResult Media()
        {
            if (Request.Files.Count == 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var model = new MediaResultViewModel();

            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];
                var media = _mediaLibraryService.ImportMedia(file.InputStream, GetMediaPath(), file.FileName);
                _orchardServices.ContentManager.Create(media);

                model.files.Add(new {
                    url = _mediaLibraryService.GetMediaPublicUrl(media.FolderPath, media.FileName)
                });
            }

            return(Json(model));
        }
        /// <summary>
        /// Creates an images thumbnail.
        /// </summary>
        /// <param name="image">The image full path on the media storage.</param>
        /// <param name="thumbnailFolderPath">The media path to thumbnails folder.</param>
        /// <param name="imageName">The image name.</param>
        /// <param name="thumbnailWidth">The thumbnail width in pixels.</param>
        /// <param name="thumbnailHeight">The thumbnail height in pixels.</param>
        /// <param name="keepAspectRatio">Indicates whether to keep the original image aspect ratio</param>
        /// <param name="expandToFill">Indicates whether to expand the thumbnail to fill the bounds specified by width and height</param>
        /// <returns>The thumbnail file media path.</returns>
        protected Thumbnail CreateThumbnail(string image, string thumbnailFolderPath, string imageName, int thumbnailWidth,
                                            int thumbnailHeight, bool keepAspectRatio, bool expandToFill)
        {
            if (thumbnailWidth <= 0)
            {
                throw new ArgumentException("Thumbnail width must be greater than zero", "thumbnailWidth");
            }

            if (thumbnailHeight <= 0)
            {
                throw new ArgumentException("Thumbnail height must be greater than zero", "thumbnailHeight");
            }

            string thumbnailFilePath = _storageProvider.Combine(thumbnailFolderPath, imageName);

            IStorageFile imageFile = _storageProvider.GetFile(image);

            using (Stream imageStream = imageFile.OpenRead()) {
                using (Image drawingImage = Image.FromStream(imageStream))
                {
                    bool shouldCreateImage = true;

                    // Verify if the image already has a Thumbnail
                    var thumbnailName = _mediaService.GetMediaFiles(thumbnailFolderPath)
                                        .Select(o => o.Name).SingleOrDefault(o => o == imageName);

                    if (thumbnailName != null)
                    {
                        // Verify if the existing thumbnail has the correct size (in case the thumbnail settings have been changed)
                        IStorageFile thumbnailFile = _storageProvider.GetFile(thumbnailFilePath);
                        using (Stream thumnailFileStream = thumbnailFile.OpenRead()) {
                            using (Image thumbnailImage = Image.FromStream(thumnailFileStream)) {
                                if (ImageHasCorrectThumbnail(drawingImage, thumbnailImage, thumbnailWidth, thumbnailHeight, keepAspectRatio, expandToFill))
                                {
                                    shouldCreateImage = false;
                                    thumbnailWidth    = thumbnailImage.Width;
                                    thumbnailHeight   = thumbnailImage.Height;
                                }
                            }
                        }
                    }

                    if (shouldCreateImage)
                    {
                        using (Image thumbDrawing = CreateThumbnail(drawingImage, thumbnailWidth, thumbnailHeight, keepAspectRatio, expandToFill)) {
                            if (_storageProvider.ListFiles(thumbnailFolderPath).Select(o => o.GetName()).Contains(imageName))
                            {
                                _storageProvider.DeleteFile(thumbnailFilePath);
                            }

                            IStorageFile thumbFile = _storageProvider.CreateFile(thumbnailFilePath);
                            using (Stream thumbStream = thumbFile.OpenWrite())
                            {
                                thumbDrawing.Save(thumbStream, _thumbnailImageFormat);
                                thumbnailWidth  = thumbDrawing.Width;
                                thumbnailHeight = thumbDrawing.Height;
                            }
                        }
                    }
                }
            }

            string thumbnailPublicUrl = _mediaService.GetMediaPublicUrl(Path.GetDirectoryName(thumbnailFilePath), Path.GetFileName(thumbnailFilePath));

            return(new Thumbnail {
                PublicUrl = thumbnailPublicUrl, Width = thumbnailWidth, Height = thumbnailHeight
            });
        }
        public MediaPartHandler(
            IStorageProvider storageProvider,
            IMediaLibraryService mediaLibraryService,
            IRepository <MediaPartRecord> repository,
            IContentDefinitionManager contentDefinitionManager)
        {
            _storageProvider          = storageProvider;
            _mediaLibraryService      = mediaLibraryService;
            _contentDefinitionManager = contentDefinitionManager;

            Filters.Add(StorageFilter.For(repository));
            Filters.Add(new ActivatingFilter <TitlePart>(contentType => {
                var typeDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
                // To avoid NRE when the handler runs for ad-hoc content types, e.g. MediaLibraryExplorer.
                return(typeDefinition == null ?
                       false :
                       typeDefinition.Parts.Any(contentTypePartDefinition =>
                                                contentTypePartDefinition.PartDefinition.Name == typeof(MediaPart).Name));
            }));

            OnRemoving <MediaPart>((context, part) => RemoveMedia(part));
            OnLoaded <MediaPart>((context, part) => {
                if (!String.IsNullOrEmpty(part.FileName))
                {
                    part._publicUrl.Loader(() => _mediaLibraryService.GetMediaPublicUrl(part.FolderPath, part.FileName));
                }
            });

            OnIndexing <MediaPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("media-folderpath", Normalize(part.FolderPath)).Store()
                                   .Add("media-filename", Normalize(part.FileName)).Store()
                                   .Add("media-mimetype", Normalize(part.MimeType)).Store()
                                   .Add("media-caption", part.Caption).Analyze()
                                   .Add("media-alternatetext", part.AlternateText).Analyze()
                                   );

            OnPublished <ImagePart>((context, part) => {
                var mediaPart = part.As <MediaPart>();
                var file      = _storageProvider.GetFile(_storageProvider.Combine(mediaPart.FolderPath, mediaPart.FileName));

                using (var stream = file.OpenRead()) {
                    try {
                        using (var image = System.Drawing.Image.FromStream(stream)) {
                            part.Width  = image.Width;
                            part.Height = image.Height;
                        }
                    }
                    catch (ArgumentException) {
                        if (mediaPart.MimeType != "image/x-icon" && mediaPart.MimeType != "image/vnd.microsoft.icon")
                        {
                            throw;
                        }
                        TryFillDimensionsForIco(stream, part);
                    }
                }
            });

            OnIndexing <ImagePart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("image-height", part.Height).Analyze().Store()
                                   .Add("image-width", part.Width).Analyze().Store()
                                   );

            OnPublished <DocumentPart>((context, part) => {
                var mediaPart = part.As <MediaPart>();
                var file      = _storageProvider.GetFile(_storageProvider.Combine(mediaPart.FolderPath, mediaPart.FileName));

                using (var stream = file.OpenRead()) {
                    part.Length = stream.Length;
                }
            });

            OnIndexing <DocumentPart>((context, part) =>
                                      context.DocumentIndex
                                      .Add("document-length", part.Length).Analyze().Store()
                                      );

            OnPublished <VideoPart>((context, part) => part.Length = 0);

            OnIndexing <VideoPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("video-length", part.Length).Analyze().Store()
                                   );

            OnPublished <AudioPart>((context, part) => part.Length = 0);

            OnIndexing <AudioPart>((context, part) =>
                                   context.DocumentIndex
                                   .Add("audio-length", part.Length).Analyze().Store()
                                   );

            OnIndexing <OEmbedPart>((context, part) =>
                                    context.DocumentIndex
                                    .Add("oembed-source", part.Source).Analyze().Store()
                                    );
        }
Beispiel #10
0
        public string RitornaParsingTemplate(dynamic contentModel, int templateId, Dictionary <string, object> viewBag = null)
        {
            try {
                if (contentModel != null && contentModel.Id != null && contentModel.Id > 0)
                {
                    _tagForCache.Add(contentModel.Id);
                }
            }
            catch { }
            if (templateId > 0)
            {
                _tagForCache.Add(templateId);
            }
            ParseTemplateContext templatectx = new ParseTemplateContext();
            var template = GetTemplate(templateId);

            var    baseUri      = new Uri(_services.WorkContext.CurrentSite.BaseUrl);
            var    basePath     = GetBasePath(baseUri);
            string host         = "";
            string mediaUrl     = "";
            var    tenantPrefix = GetTenantUrlPrexix(_shellSettings);

            if (_services.WorkContext.HttpContext != null)
            {
                var urlHelper = new UrlHelper(_services.WorkContext.HttpContext.Request.RequestContext);

                // Creo un model che ha Content (il contentModel), Urls con alcuni oggetti utili per il template
                // Nel template pertanto Model, diventa Model.Content
                host = string.Format("{0}://{1}{2}",
                                     _services.WorkContext.HttpContext.Request.Url.Scheme,
                                     _services.WorkContext.HttpContext.Request.Url.Host,
                                     _services.WorkContext.HttpContext.Request.Url.Port == 80
                                     ? string.Empty
                                     : ":" + _services.WorkContext.HttpContext.Request.Url.Port);
                mediaUrl = urlHelper.MediaExtensionsImageUrl();
            }
            else
            {
                host = string.Format("{0}://{1}{2}",
                                     baseUri.Scheme,
                                     baseUri.Host,
                                     baseUri.Port == 80 ? string.Empty : ":" + baseUri.Port);
                mediaUrl = string.Format("{0}{1}{2}", basePath, tenantPrefix, @"Laser.Orchard.StartupConfig/MediaTransform/Image");
            }
            // compute base url for medias in the media folder:
            var mockMediaPath = _mediaLibraryService
                                .GetMediaPublicUrl("a", "a"); // path for mocked media
            var baseMediaPath = Path.Combine(host,
                                             mockMediaPath
                                             .Substring(0, mockMediaPath.Length - 3));
            string baseUrl      = string.Format("{0}{1}{2}", host, basePath, tenantPrefix);
            var    dynamicModel = new {
                WorkContext = _services.WorkContext,
                Content     = contentModel,
                Urls        = new {
                    BaseUrl         = baseUrl,
                    MediaUrl        = mediaUrl,
                    Domain          = host,
                    PublicMediaPath = baseMediaPath,
                }.ToExpando()
            };

            templatectx.Model = dynamicModel;

            //  var razorviewBag = viewBag;
            //RazorEngine.Templating.DynamicViewBag vb = new DynamicViewBag();
            //try {
            //    foreach (string key in ((Dictionary<string, object>)viewBag).Keys) {
            //        vb.AddValue(key, ((IDictionary<string, object>)viewBag)[key]);
            //    }
            //} catch { }
            //templatectx.ViewBag = vb;
            templatectx.ViewBag = viewBag;

            return(ParseTemplate(template, templatectx));
        }
 private String GetIconFolder()
 {
     return(_mediaLibraryService.GetMediaPublicUrl(BingMapListService.MAPICONS_MEDIA_FOLDER + "/", ""));
 }