public ActionResult DownloadFile(Guid guid, string name)
        {
            var fileRecord = fileRepository.Table.FirstOrDefault(c => c.FolderGuid == guid);

            if (fileRecord != null)
            {
                var item = _services.ContentManager.Get(fileRecord.ContentItemRecord.Id);
                if (item != null)
                {
                    if (!item.Has <FileUploadPart>())
                    {
                        return(HttpNotFound());
                    }

                    if (!this.contentOwnershipService.CurrentUserCanEditContent(item))
                    {
                        throw new UnauthorizedAccessException();
                    }
                }
            }
            else
            {
                return(HttpNotFound());
            }

            var folders = _mediaService.GetMediaFolders("Uploads");
            var folder  = folders.SingleOrDefault(x => x.Name == guid.ToString());

            if (folder == null)
            {
                return(HttpNotFound());
            }

            var file = _mediaService.GetMediaFiles(folder.MediaPath).SingleOrDefault(x => x.Name == name);

            if (file == null)
            {
                return(HttpNotFound());
            }

            var path = HostingEnvironment.MapPath(Path.Combine("~/Media/Default/", folder.MediaPath, file.Name));

            byte[] bytes = null;
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var reader = new BinaryReader(stream);
                bytes = reader.ReadBytes((int)stream.Length);
            }

            return(File(bytes, file.Type, file.Name));
        }
Beispiel #2
0
        public ImageGalleryImage GetImage(string imageGalleryName, string imageName)
        {
            string imageGalleryMediaPath = GetMediaPath(imageGalleryName);
            ImageGallerySettingsRecord imageGallerySettings = GetImageGallerySettings(imageGalleryMediaPath);

            MediaFile file = _mediaService.GetMediaFiles(imageGalleryMediaPath)
                             .SingleOrDefault(mediaFile => mediaFile.Name == imageName);

            if (file == null)
            {
                return(null);
            }

            return(CreateImageFromMediaFile(file, imageGallerySettings));
        }
        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);
        }
        private DriverResult FilesList(FileUploadPart part, dynamic shapeHelper, out int count)
        {
            count = 0;
            if (part.Guid == Guid.Empty)
            {
                return(null);
            }

            var    settings = part.TypePartDefinition.GetFileUploadPartSettings();
            string url;

            if (!string.IsNullOrEmpty(settings.PublicMediaPath))
            {
                url = _tokenizer.Replace(settings.PublicMediaPath, new { Content = part.ContentItem });
            }
            else
            {
                url = part.Guid.ToString();
            }

            List <FileDisplayViewModel> files = new List <FileDisplayViewModel>();

            bool   currentUserHasEditAccess = this.crmContentOwnershipService.CurrentUserCanEditContent(part);
            string folder = "Uploads/" + part.Guid;

            if (this.storageProvider.FolderExists(folder))
            {
                files = _mediaService.GetMediaFiles(folder).Select(file =>
                {
                    var routeValues = new RouteValueDictionary();
                    routeValues.Add("id", part.Id);
                    routeValues.Add("guid", part.Record.FolderGuid);
                    routeValues.Add("name", file.Name);
                    routeValues.Add("area", "Orchard.CRM.Core");
                    return(new FileDisplayViewModel {
                        Name = file.Name, RouteValues = routeValues, Uploaded = file.LastUpdated
                    });
                }).ToList();

                count = files.Count;
            }

            return(ContentShape("Parts_FilesList", () => shapeHelper.Parts_FilesList(Files: files, CurrentUserHasEditAccess: currentUserHasEditAccess)));
        }
        private IEnumerable <String> GetPossibleMapIcons()
        {
            // Get possible icons
            List <String> possibleMapIcons;
            var           rootMediaFolders = _mediaLibraryService
                                             .GetMediaFolders(".")
                                             .Where(f => f.Name.Equals(BingMapListService.MAPICONS_MEDIA_FOLDER, StringComparison.OrdinalIgnoreCase));

            if (rootMediaFolders.Any())
            {
                possibleMapIcons = new List <string>(
                    _mediaLibraryService.GetMediaFiles(BingMapListService.MAPICONS_MEDIA_FOLDER)
                    .Select(f => f.Name));
            }
            else
            {
                possibleMapIcons = new List <String>();
            }

            return(possibleMapIcons);
        }
        /// <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 ActionResult DeleteFile(Guid guid, string name, string returnValue)
        {
            var         fileRecord = fileRepository.Table.FirstOrDefault(c => c.FolderGuid == guid);
            ContentItem item       = null;

            if (fileRecord != null)
            {
                item = _services.ContentManager.Get(fileRecord.ContentItemRecord.Id);
                if (item != null)
                {
                    if (!item.Has <FileUploadPart>())
                    {
                        return(HttpNotFound());
                    }

                    if (!this.contentOwnershipService.CurrentUserCanEditContent(item))
                    {
                        throw new UnauthorizedAccessException();
                    }
                }
            }

            var folders = _mediaService.GetMediaFolders("Uploads");
            var folder  = folders.SingleOrDefault(x => x.Name == guid.ToString());

            if (folder == null)
            {
                return(HttpNotFound());
            }

            if (this.storageProvider.FileExists(this.storageProvider.Combine(folder.MediaPath, name)))
            {
                _mediaService.DeleteFile(folder.MediaPath, name);
            }

            fileRecord.FilesCount = _mediaService.GetMediaFiles(folder.MediaPath).Count();

            if (item != null)
            {
                int    userId      = _services.WorkContext.CurrentUser.Id;
                string description = string.Format(CultureInfo.CurrentUICulture, "Delete the file '{0}' from '{1}'", name, this.GetContentItemDescriptionForActivityStream(item));
                RouteValueDictionary routeValueDictionary = new RouteValueDictionary();
                routeValueDictionary.Add("action", "Display");
                routeValueDictionary.Add("controller", "Item");
                routeValueDictionary.Add("area", "Orchard.CRM.Core");
                routeValueDictionary.Add("id", fileRecord.ContentItemRecord.Id);
                this.activityStreamService.WriteChangesToStreamActivity(userId, item.Id, item.VersionRecord.Id, new ActivityStreamChangeItem[] { }, description, routeValueDictionary);
            }

            if (Request.IsAjaxRequest())
            {
                return(this.Json(new { IsDone = true }, JsonRequestBehavior.AllowGet));
            }
            else if (!string.IsNullOrEmpty(returnValue))
            {
                return(Redirect(returnValue));
            }
            else if (fileRecord != null)
            {
                return(this.RedirectToAction("Display", "Ticket", new { area = "Orchard.CRM.Core", id = fileRecord.ContentItemRecord.Id }));
            }
            else
            {
                return(this.RedirectToAction("Search", "Ticket", new { area = "Orchard.CRM.Core" }));
            }
        }