Ejemplo n.º 1
0
        /// <summary>
        /// Uploads a file. If Asset.Key is not provided then one is created using the RootFolder and Asset.Name.
        /// If a key is provided it MUST use the full path, RootFolder is not used.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset">The asset.</param>
        /// <returns></returns>
        public override bool UploadObject(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));

            asset.Key = FixKey(asset, rootFolder);

            if (!IsFileTypeAllowedByBlackAndWhiteLists(asset.Key))
            {
                string ext = System.IO.Path.GetExtension(asset.Key);
                var    ex  = new Rock.Web.FileUploadException($"Filetype {ext} is not allowed.", System.Net.HttpStatusCode.NotAcceptable);
                ExceptionLogService.LogException(ex);
                throw ex;
            }

            try
            {
                string physicalPath = FileSystemComponentHttpContext.Server.MapPath(asset.Key);

                using (FileStream fs = new FileStream(physicalPath, FileMode.Create))
                    using (asset.AssetStream)
                    {
                        asset.AssetStream.CopyTo(fs);
                    }

                return(true);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renames the asset.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset">The asset.</param>
        /// <param name="newName">The new name.</param>
        /// <returns></returns>
        public override bool RenameAsset(AssetStorageProvider assetStorageProvider, Asset asset, string newName)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));

            if (!IsFileTypeAllowedByBlackAndWhiteLists(newName))
            {
                string ext = System.IO.Path.GetExtension(asset.Key);
                var    ex  = new Rock.Web.FileUploadException($"Filetype {ext} is not allowed.", System.Net.HttpStatusCode.NotAcceptable);
                ExceptionLogService.LogException(ex);
                throw ex;
            }

            try
            {
                asset.Key = FixKey(asset, rootFolder);
                string filePath        = GetPathFromKey(asset.Key);
                string physicalFolder  = FileSystemComponentHttpContext.Server.MapPath(filePath);
                string physicalFile    = FileSystemComponentHttpContext.Server.MapPath(asset.Key);
                string newPhysicalFile = Path.Combine(physicalFolder, newName);

                File.Move(physicalFile, newPhysicalFile);
                DeleteImageThumbnail(assetStorageProvider, asset);

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }