Esempio n. 1
0
        /// <summary>
        /// Gets the object as an Asset with the option to create a thumbnail.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="createThumbnail">if set to <c>true</c> [create thumbnail].</param>
        /// <returns></returns>
        public override Asset GetObject(AssetStorageProvider assetStorageProvider, Asset asset, bool createThumbnail)
        {
            try
            {
                string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));

                asset.Key = FixKey(asset, rootFolder);
                string   physicalFile = FileSystemComponentHttpContext.Server.MapPath(asset.Key);
                FileInfo fileInfo     = new FileInfo(physicalFile);

                var objAsset = CreateAssetFromFileInfo(assetStorageProvider, fileInfo, createThumbnail);
                using (FileStream fs = new FileStream(physicalFile, FileMode.Open))
                {
                    objAsset.AssetStream = new MemoryStream();
                    fs.CopyTo(objAsset.AssetStream);
                }

                return(objAsset);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
Esempio n. 2
0
        private void ProcessAssetStorageProviderAsset(HttpContext context, HttpPostedFile uploadedFile)
        {
            int?   assetStorageId = context.Request.Form["StorageId"].AsIntegerOrNull();
            string assetKey       = context.Request.Form["Key"] + uploadedFile.FileName;

            if (assetStorageId == null || assetKey.IsNullOrWhiteSpace())
            {
                throw new Rock.Web.FileUploadException("Insufficient info to upload a file of this type.", System.Net.HttpStatusCode.Forbidden);
            }

            var assetStorageService = new AssetStorageProviderService(new RockContext());
            AssetStorageProvider assetStorageProvider = assetStorageService.Get((int)assetStorageId);

            assetStorageProvider.LoadAttributes();
            var component = assetStorageProvider.GetAssetStorageComponent();

            var asset = new Rock.Storage.AssetStorage.Asset();

            asset.Key         = assetKey;
            asset.Type        = Rock.Storage.AssetStorage.AssetType.File;
            asset.AssetStream = uploadedFile.InputStream;

            if (component.UploadObject(assetStorageProvider, asset))
            {
                context.Response.Write(new { Id = string.Empty, FileName = assetKey }.ToJson());
            }
            else
            {
                throw new Rock.Web.FileUploadException("Unable to upload file", System.Net.HttpStatusCode.BadRequest);
            }
        }
Esempio n. 3
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)
        {
            try
            {
                var originalAsset = GetObject(assetStorageProvider, asset);

                if (originalAsset.Name.Equals(newName, StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                var newAsset = originalAsset.Clone();
                newAsset.Name = newName;

                var path = GetPathFromKey(originalAsset.Key);
                newAsset.Key = $"{path}{newName}";

                UploadObject(assetStorageProvider, newAsset);
                DeleteAsset(assetStorageProvider, originalAsset);

                return(true);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the asset from blob object.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="blobItem">The blob object.</param>
        /// <param name="createThumbnail">if set to <c>true</c> [create thumbnail].</param>
        /// <returns></returns>
        private Asset TranslateBlobToRockAsset(AssetStorageProvider assetStorageProvider, CloudBlob blobItem, bool createThumbnail = true)
        {
            var asset = new Asset
            {
                Name = GetNameFromKey(blobItem.Name),
                Key  = blobItem.Name,
                Uri  = blobItem.Uri.ToString(),
                Type = AssetType.File,
                AssetStorageProviderId = assetStorageProvider.Id
            };

            if (blobItem.Properties != null)
            {
                asset.FileSize    = blobItem.Properties.Length;
                asset.Description = $"{blobItem.Properties.Length} byte{( blobItem.Properties.Length == 1 ? string.Empty : "s" )}";
                if (blobItem.Properties.LastModified != null)
                {
                    asset.LastModifiedDateTime = RockDateTime.ConvertLocalDateTimeToRockDateTime(blobItem.Properties.LastModified.Value.LocalDateTime);
                }
            }

            if (createThumbnail)
            {
                asset.IconPath = createThumbnail ?
                                 GetThumbnail(assetStorageProvider, blobItem.Name, asset.LastModifiedDateTime) :
                                 GetFileTypeIcon(blobItem.Name);
            }
            return(asset);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a folder. If Asset.Key is not provided then one is created using the RootFolder and Asset.Name.
        /// If Key is provided it MUST use the full path, RootFolder is not used.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset"></param>
        /// <returns></returns>
        public override bool CreateFolder(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, AttributeKey.RootFolder));

            asset.Key = FixKey(asset, rootFolder);

            try
            {
                var container = GetCloudBlobContainer(assetStorageProvider);

                // Retrieve reference to a blob named "myblob".
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(asset.Key + "/" + DEFAULT_FILE_NAME);
                if (!blockBlob.Exists())
                {
                    blockBlob.UploadText(string.Empty);
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }

            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// Returns an asset with the stream of the specified file with the option to create a thumbnail.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="createThumbnail">if set to <c>true</c> [create thumbnail].</param>
        /// <returns></returns>
        public override Asset GetObject(AssetStorageProvider assetStorageProvider, Asset asset, bool createThumbnail)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, AttributeKey.RootFolder));

            asset.Key = FixKey(asset, rootFolder);
            HasRequirementsFile(asset);

            try
            {
                var container = GetCloudBlobContainer(assetStorageProvider);

                // Get a reference to a blob with a request to the server.
                // If the blob does not exist, this call will fail with a 404 (Not Found).
                var blob = container.GetBlobReferenceFromServer(asset.Key) as CloudBlob;

                var responseAsset = TranslateBlobToRockAsset(assetStorageProvider, blob as CloudBlob, createThumbnail);

                responseAsset.AssetStream = new MemoryStream();
                blob.DownloadToStream(responseAsset.AssetStream);
                responseAsset.AssetStream.Position = 0;

                return(responseAsset);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
        /// <summary>
        /// Creates the image thumbnail from stream.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="asset">The asset.</param>
        /// <param name="physicalThumbPath">The physical thumb path.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        private void CreateImageThumbnailFromStream(AssetStorageProvider assetStorageProvider, Asset asset, string physicalThumbPath, int?width = null, int?height = null)
        {
            asset = GetObject(assetStorageProvider, asset, false);

            using (var resizedStream = new FileStream(physicalThumbPath, FileMode.Create))
            {
                if (Path.GetExtension(asset.Name).Equals(".svg", StringComparison.OrdinalIgnoreCase))
                {
                    // just save the svg to the thumbnail dir
                    asset.AssetStream.CopyTo(resizedStream);
                }
                else
                {
                    using (var origImageStream = new MemoryStream())
                    {
                        asset.AssetStream.CopyTo(origImageStream);
                        origImageStream.Position = 0;
                        ImageResizer.ImageBuilder.Current.Build(origImageStream, resizedStream, new ImageResizer.ResizeSettings {
                            Width = width ?? 100, Height = height ?? 100
                        });
                    }
                }

                resizedStream.Flush();
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Overridden to take JSON input of AssetStorageID and Key and create a URL. If the asset is using Amazon then a presigned URL is
        /// created.
        /// </summary>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="value">Information about the value</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param>
        /// <returns></returns>
        public override string FormatValue(Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed)
        {
            Storage.AssetStorage.Asset asset = GetAssetInfoFromValue(value);

            if (asset == null)
            {
                return(string.Empty);
            }

            AssetStorageProvider assetStorageProvider = new AssetStorageProvider();
            int?assetStorageId = asset.AssetStorageProviderId;

            if (assetStorageId != null)
            {
                var assetStorageService = new AssetStorageProviderService(new RockContext());
                assetStorageProvider = assetStorageService.Get(assetStorageId.Value);
                assetStorageProvider.LoadAttributes();
            }

            var component = assetStorageProvider.GetAssetStorageComponent();

            string uri = component.CreateDownloadLink(assetStorageProvider, asset);

            return(uri);
        }
        /// <summary>
        /// Creates the download link.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset">The asset.</param>
        /// <returns></returns>
        public override string CreateDownloadLink(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));

            asset.Key = FixKey(asset, rootFolder);

            try
            {
                AmazonS3Client client = GetAmazonS3Client(assetStorageProvider);

                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = GetAttributeValue(assetStorageProvider, "Bucket"),
                    Key        = asset.Key,
                    Expires    = DateTime.Now.AddMinutes(GetAttributeValue(assetStorageProvider, "Expiration").AsDouble())
                };

                return(client.GetPreSignedURL(request));
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
        /// <summary>
        /// Handles the Click event of the lbDownload control.
        /// Downloads the file and propts user to save or open.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbDownload_Click(object sender, EventArgs e)
        {
            AssetStorageProvider assetStorageProvider = GetAssetStorageProvider();
            var component = assetStorageProvider.GetAssetStorageComponent();

            foreach (RepeaterItem file in rptFiles.Items)
            {
                var cbEvent = file.FindControl("cbSelected") as RockCheckBox;
                if (cbEvent.Checked == true)
                {
                    var    keyControl = file.FindControl("lbKey") as Label;
                    string key        = keyControl.Text;
                    Asset  asset      = component.GetObject(assetStorageProvider, new Asset {
                        Key = key, Type = AssetType.File
                    }, false);

                    byte[] bytes = asset.AssetStream.ReadBytesToEnd();

                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("content-disposition", "attachment; filename=" + asset.Name);
                    Response.BufferOutput = true;
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.SuppressContent = true;
                    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Creates a folder. If Asset.Key is not provided then one is created using the RootFolder and Asset.Name.
        /// If Key is provided it MUST use the full path, RootFolder is not used.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset"></param>
        /// <returns></returns>
        public override bool CreateFolder(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, AttributeKeys.RootFolder));

            asset.Key = FixKey(asset, rootFolder);

            try
            {
                AmazonS3Client client = GetAmazonS3Client(assetStorageProvider);

                PutObjectRequest request = new PutObjectRequest();
                request.BucketName = GetAttributeValue(assetStorageProvider, AttributeKeys.Bucket);
                request.Key        = asset.Key;

                PutObjectResponse response = client.PutObject(request);
                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }

            return(false);
        }
Esempio n. 12
0
        /// <summary>
        /// Lists the objects from the current root folder.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <returns></returns>
        public override List <Asset> ListObjects(AssetStorageProvider assetStorageProvider)
        {
            var asset = new Asset();

            asset.Type = AssetType.Folder;
            return(ListObjects(assetStorageProvider, asset));
        }
Esempio n. 13
0
        /// <summary>
        /// Creates the download link.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset">The asset.</param>
        /// <returns></returns>
        public override string CreateDownloadLink(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, AttributeKeys.RootFolder));

            asset.Key = FixKey(asset, rootFolder);
            string url = string.Empty;

            try
            {
                AmazonS3Client client = GetAmazonS3Client(assetStorageProvider);

                GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = GetAttributeValue(assetStorageProvider, AttributeKeys.Bucket),
                    Key        = asset.Key,
                    Expires    = RockInstanceConfig.SystemDateTime.AddMinutes(GetAttributeValue(assetStorageProvider, AttributeKeys.Expiration).AsDouble())
                };

                url = client.GetPreSignedURL(request);

                if (GetAttributeValue(assetStorageProvider, AttributeKeys.GenerateSingedURLs).AsBooleanOrNull() ?? false)
                {
                    return(url);
                }
                else
                {
                    return(url.Left(url.IndexOf('?')));
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Gets the list of objects.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="directoryName">Name of the directory.</param>
        /// <param name="searchOption">The search option.</param>
        /// <param name="assetType">Type of the asset.</param>
        /// <returns></returns>
        private List <Asset> GetListOfObjects(AssetStorageProvider assetStorageProvider, string directoryName, SearchOption searchOption, AssetType assetType)
        {
            List <Asset> assets        = new List <Asset>();
            var          baseDirectory = new DirectoryInfo(directoryName);

            if (assetType == AssetType.Folder)
            {
                var directoryInfos = baseDirectory.GetDirectories("*", searchOption);

                foreach (var directoryInfo in directoryInfos)
                {
                    if (!HiddenFolders.Any(a => directoryInfo.FullName.IndexOf(a, StringComparison.OrdinalIgnoreCase) > 0))
                    {
                        var asset = CreateAssetFromDirectoryInfo(directoryInfo);
                        assets.Add(asset);
                    }
                }
            }
            else
            {
                var fileInfos = baseDirectory.GetFiles("*", searchOption);

                foreach (var fileInfo in fileInfos)
                {
                    var asset = CreateAssetFromFileInfo(assetStorageProvider, fileInfo, true);
                    assets.Add(asset);
                }
            }

            return(assets);
        }
Esempio n. 15
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;
            }
        }
        /// <summary>
        /// Handles the Click event of the lbCreateFolderAccept control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void lbCreateFolderAccept_Click(object sender, EventArgs e)
        {
            if (!IsValidName(tbCreateFolder.Text) || tbCreateFolder.Text.IsNullOrWhiteSpace())
            {
                return;
            }

            AssetStorageProvider assetStorageProvider = GetAssetStorageProvider();
            var component = assetStorageProvider.GetAssetStorageComponent();
            var asset     = new Asset {
                Type = AssetType.Folder
            };

            // Selecting the root does not put a value for the selected folder, so we have to make sure
            // if it does not have a value that we use name instead of key so the root folder is used
            // by the component.
            if (hfSelectFolder.Value.IsNotNullOrWhiteSpace())
            {
                asset.Key = hfSelectFolder.Value + tbCreateFolder.Text;
            }
            else
            {
                asset.Name = tbCreateFolder.Text;
            }

            component.CreateFolder(assetStorageProvider, asset);
            upnlFolders.Update();
        }
        /// <summary>
        /// Deletes the image thumbnail for the provided Asset. If the asset is a file then the singel thumbnail
        /// is deleted. If the asset is a directory then a recurrsive delete is done.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="asset">The asset.</param>
        protected virtual void DeleteImageThumbnail(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string cleanKey     = asset.Key.TrimStart('~');
            string virtualPath  = $"{ThumbnailRootPath}/{assetStorageProvider.Id}/{cleanKey}";
            string physicalPath = FileSystemCompontHttpContext.Server.MapPath(virtualPath);

            try
            {
                if (asset.Type == AssetType.File)
                {
                    if (File.Exists(physicalPath))
                    {
                        File.Delete(physicalPath);
                    }
                }
                else if (asset.Type == AssetType.Folder)
                {
                    if (Directory.Exists(physicalPath))
                    {
                        Directory.Delete(physicalPath, true);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Deletes the asset. If Asset.Key is not provided then one is created using the RootFolder and Asset.Name.
        /// If Key is provided then 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 DeleteAsset(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            try
            {
                string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));
                asset.Key = FixKey(asset, rootFolder);
                string physicalPath = FileSystemComponentHttpContext.Server.MapPath(asset.Key);

                if (asset.Type == AssetType.File)
                {
                    if (File.Exists(physicalPath))
                    {
                        File.Delete(physicalPath);
                    }
                }
                else
                {
                    if (Directory.Exists(physicalPath))
                    {
                        Directory.Delete(physicalPath, true);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }

            DeleteImageThumbnail(assetStorageProvider, asset);

            return(true);
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                AssetStorageProvider assetStorageProvider = null;
                var assetStorageProviderService           = new AssetStorageProviderService(rockContext);

                if (AssetStorageProviderId != 0)
                {
                    assetStorageProvider = assetStorageProviderService.Get(AssetStorageProviderId);
                }

                if (assetStorageProvider == null)
                {
                    assetStorageProvider = new Rock.Model.AssetStorageProvider();
                    assetStorageProviderService.Add(assetStorageProvider);
                }

                assetStorageProvider.Name         = tbName.Text;
                assetStorageProvider.IsActive     = cbIsActive.Checked;
                assetStorageProvider.Description  = tbDescription.Text;
                assetStorageProvider.EntityTypeId = cpAssetStorageType.SelectedEntityTypeId;

                rockContext.SaveChanges();

                assetStorageProvider.LoadAttributes(rockContext);
                Rock.Attribute.Helper.GetEditValues(phAttributes, assetStorageProvider);
                assetStorageProvider.SaveAttributeValues(rockContext);
            }

            NavigateToParentPage();
        }
Esempio n. 20
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, AttributeKeys.RootFolder));

            asset.Key = asset.Key.IsNullOrWhiteSpace() ? rootFolder + asset.Name : asset.Key;
            string bucket = GetAttributeValue(assetStorageProvider, AttributeKeys.Bucket);

            try
            {
                AmazonS3Client client = GetAmazonS3Client(assetStorageProvider);

                CopyObjectRequest copyRequest = new CopyObjectRequest();
                copyRequest.SourceBucket      = bucket;
                copyRequest.DestinationBucket = bucket;
                copyRequest.SourceKey         = asset.Key;
                copyRequest.DestinationKey    = GetPathFromKey(asset.Key) + newName;
                CopyObjectResponse copyResponse = client.CopyObject(copyRequest);
                if (copyResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(false);
                }

                if (DeleteAsset(assetStorageProvider, asset))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex);
                throw;
            }

            return(false);
        }
Esempio n. 21
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;
            }
        }
Esempio n. 22
0
        protected AssetStorageProvider GetAssetStorageProvider()
        {
            var assetStorageProviderService           = new AssetStorageProviderService(new RockContext());
            AssetStorageProvider assetStorageProvider = assetStorageProviderService.Get(_assetStorageProviderServiceGuid);

            assetStorageProvider.LoadAttributes();
            return(assetStorageProvider);
        }
Esempio n. 23
0
        /// <summary>
        /// Gets the assets from Google.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="directory">The directory.</param>
        /// <param name="assetTypeToList">The asset type to list.</param>
        /// <param name="allowRecursion">if set to <c>true</c> [allow recursion].</param>
        /// <returns></returns>
        private List <GoogleObject> GetObjectsFromGoogle(AssetStorageProvider assetStorageProvider, string directory, AssetType?assetTypeToList, bool allowRecursion)
        {
            var bucketName     = GetBucketName(assetStorageProvider);
            var accountKeyJson = GetServiceAccountKeyJson(assetStorageProvider);

            return(GoogleCloudStorage.GetObjectsFromGoogle(bucketName, accountKeyJson, directory, assetTypeToList == AssetType.File,
                                                           assetTypeToList == AssetType.Folder, allowRecursion));
        }
        /// <summary>
        /// Handles the SelectedIndexChanged event of the cpAssetStorageType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void cpAssetStorageType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var assetStorageProvider = new AssetStorageProvider {
                Id = AssetStorageProviderId, EntityTypeId = cpAssetStorageType.SelectedEntityTypeId
            };

            BuildDynamicControls(assetStorageProvider, true);
        }
Esempio n. 25
0
        /// <summary>
        /// Lists the objects in folder. The asset key or name should be the folder.
        /// If Asset.Key is not provided then one is created using the RootFolder and Asset.Name
        /// If Key and Name are not provided then list all objects in the current RootFolder.
        /// If a key is provided it MUST use the full path, RootFolder and Name are not used.
        /// The last segment in key is the folder name.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="asset">The asset.</param>
        /// <returns></returns>
        public override List <Asset> ListObjectsInFolder(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            var rootFolder = GetRootFolder(assetStorageProvider);

            FixKey(asset, rootFolder);

            return(GetAssetsFromGoogle(assetStorageProvider, asset.Key, null, false));
        }
Esempio n. 26
0
        /// <summary>
        /// Creates the asset from AWS S3 Client GetObjectResponse.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <param name="response">The response.</param>
        /// <param name="regionEndpoint">The region endpoint.</param>
        /// <param name="createThumbnail">if set to <c>true</c> [create thumbnail].</param>
        /// <returns></returns>
        private Asset CreateAssetFromGetObjectResponse(AssetStorageProvider assetStorageProvider, GetObjectResponse response, string regionEndpoint, bool createThumbnail)
        {
            string name   = GetNameFromKey(response.Key);
            string uriKey = System.Web.HttpUtility.UrlPathEncode(response.Key);

            /*
             *   2/24/2021 - NA
             *
             *   The response.ResponseStream wrapper class appears to change based
             *   on file (perhaps the file size?) so in certain cases you cannot read
             *   the response.ResponseStream.Length. But, the response.ContentLength
             *   appears to *always* be available (and it matches the response.ResponseStream.Length
             *   when it was available during my tests).
             *
             *   Reason: Amazon S3's Amazon.Runtime.Internal.Util.WrapperStream changes based on the file.
             */

            Asset asset = new Asset();

            try
            {
                asset = new Asset
                {
                    Name     = name,
                    Key      = response.Key,
                    Uri      = $"https://{response.BucketName}.s3.{regionEndpoint}.amazonaws.com/{uriKey}",
                    Type     = GetAssetType(response.Key),
                    IconPath = createThumbnail == true?GetThumbnail(assetStorageProvider, response.Key, response.LastModified) : GetFileTypeIcon(response.Key),
                                   FileSize             = response.ContentLength,
                                   LastModifiedDateTime = response.LastModified,
                                   Description          = response.StorageClass == null ? string.Empty : response.StorageClass.ToString(),
                                   AssetStream          = response.ResponseStream,
                                   HasError             = true
                };
            }
            catch (Exception ex)
            {
                // log the exception and create an assett that describes the error, but don't throw it.
                ExceptionLogService.LogException(new AggregateException($"Error creating asset for key {response.Key}", ex));
                asset = new Asset
                {
                    Name                 = name,
                    Key                  = response.Key,
                    Uri                  = $"https://{response.BucketName}.s3.{regionEndpoint}.amazonaws.com/{uriKey}",
                    Type                 = GetAssetType(response.Key),
                    IconPath             = GetCorruptImageAssetImage(),
                    FileSize             = response.ContentLength,
                    LastModifiedDateTime = response.LastModified,
                    Description          = response.StorageClass == null ? string.Empty : response.StorageClass.ToString(),
                    AssetStream          = response.ResponseStream,
                    HasError             = true
                };
            }



            return(asset);
        }
Esempio n. 27
0
        private static void CreateAssetStorageProvider(TestContext testContext)
        {
            AssetStorageProvider assetStorageProvider = null;

            var bucketName = testContext.Properties["GoogleBucketName"].ToStringSafe();
            var apiKey     = testContext.Properties["GoogleServiceAccountJsonKey"].ToStringSafe();

            // Just Assert Inconclusive if the GoogleStorageAccountName and GoogleAccountAccessKey are blank
            if (string.IsNullOrEmpty(bucketName) || string.IsNullOrEmpty(apiKey))
            {
                Assert.That.Inconclusive($"The GoogleStorageAccountName and GoogleAccountAccessKey must be set up in your Test > Test Settings > Test Setting File in order to run these tests.");
            }

            try
            {
                using (var rockContext = new RockContext())
                {
                    var assetStorageProviderService = new AssetStorageProviderService(rockContext);
                    assetStorageProvider = assetStorageProviderService.Get(_assetStorageProviderServiceGuid);

                    if (assetStorageProvider == null)
                    {
                        // This is the registered Guid for the 'Rock.Storage.AssetStorage.GoogleCloudStorageComponent' entity type
                        var entityType = EntityTypeCache.Get(Rock.SystemGuid.EntityType.STORAGE_ASSETSTORAGE_GOOGLECLOUD.AsGuid());

                        assetStorageProvider              = new AssetStorageProvider();
                        assetStorageProvider.Name         = "TEST Google Cloud AssetStorageProvider";
                        assetStorageProvider.Guid         = _assetStorageProviderServiceGuid;
                        assetStorageProvider.EntityTypeId = entityType.Id;
                        assetStorageProvider.IsActive     = true;

                        assetStorageProviderService.Add(assetStorageProvider);

                        rockContext.SaveChanges();

                        var assetStorageProviderComponentEntityType = EntityTypeCache.Get(assetStorageProvider.EntityTypeId.Value);
                        var assetStorageProviderEntityType          = EntityTypeCache.Get <Rock.Model.AssetStorageProvider>();

                        Helper.UpdateAttributes(
                            assetStorageProviderComponentEntityType.GetEntityType(),
                            assetStorageProviderEntityType.Id,
                            "EntityTypeId",
                            assetStorageProviderComponentEntityType.Id.ToString(),
                            rockContext);
                    }

                    assetStorageProvider.LoadAttributes();
                    assetStorageProvider.SetAttributeValue("BucketName", bucketName);
                    assetStorageProvider.SetAttributeValue("ApiKey", Encryption.EncryptString(apiKey));
                    assetStorageProvider.SetAttributeValue("RootFolder", testContext.Properties["UnitTestRootFolder"].ToStringSafe());
                    assetStorageProvider.SaveAttributeValues(rockContext);
                }
            }
            catch (System.Exception ex)
            {
                Assert.That.Inconclusive($"Unable to get the Google Cloud Asset Storage Provider ({ex.Message}).");
            }
        }
Esempio n. 28
0
        /// <summary>
        /// Returns an Amazon S3 Client obj using the settings in the provided AssetStorageProvider obj.
        /// </summary>
        /// <param name="assetStorageProvider">The asset storage provider.</param>
        /// <returns></returns>
        private AmazonS3Client GetAmazonS3Client(AssetStorageProvider assetStorageProvider)
        {
            string         awsAccessKey   = GetAttributeValue(assetStorageProvider, AttributeKeys.AWSAccessKey);
            string         awsSecretKey   = GetAttributeValue(assetStorageProvider, AttributeKeys.AWSSecretKey);
            string         awsRegion      = GetAttributeValue(assetStorageProvider, AttributeKeys.AWSRegion);
            RegionEndpoint regionEndPoint = Amazon.RegionEndpoint.GetBySystemName(awsRegion);

            return(new AmazonS3Client(awsAccessKey, awsSecretKey, regionEndPoint));
        }
Esempio n. 29
0
        private static void CreateAssetStorageProvider(TestContext testContext)
        {
            AssetStorageProvider assetStorageProvider = null;

            var accessKey = testContext.Properties["AWSAccessKey"].ToStringSafe();
            var secretKey = testContext.Properties["AWSSecretKey"].ToStringSafe();

            try
            {
                using (var rockContext = new RockContext())
                {
                    var assetStorageProviderService = new AssetStorageProviderService(rockContext);
                    assetStorageProvider = assetStorageProviderService.Get(_assetStorageProviderServiceGuid);

                    if (assetStorageProvider == null)
                    {
                        var entityType = EntityTypeCache.Get(Rock.SystemGuid.EntityType.STORAGE_ASSETSTORAGE_AMAZONS3.AsGuid());

                        assetStorageProvider              = new AssetStorageProvider();
                        assetStorageProvider.Name         = "TEST Amazon S3 AssetStorageProvider";
                        assetStorageProvider.Guid         = _assetStorageProviderServiceGuid;
                        assetStorageProvider.EntityTypeId = entityType.Id;
                        assetStorageProvider.IsActive     = true;

                        assetStorageProviderService.Add(assetStorageProvider);

                        rockContext.SaveChanges();

                        var assetStorageProviderComponentEntityType = EntityTypeCache.Get(assetStorageProvider.EntityTypeId.Value);
                        var assetStorageProviderEntityType          = EntityTypeCache.Get <Rock.Model.AssetStorageProvider>();

                        Helper.UpdateAttributes(
                            assetStorageProviderComponentEntityType.GetEntityType(),
                            assetStorageProviderEntityType.Id,
                            "EntityTypeId",
                            assetStorageProviderComponentEntityType.Id.ToString(),
                            rockContext);
                    }

                    assetStorageProvider.LoadAttributes();
                    assetStorageProvider.SetAttributeValue("GenerateSingedURLs", "False");   // The attribute Key has that typo.
                    assetStorageProvider.SetAttributeValue("AWSRegion", testContext.Properties["AWSRegion"].ToStringSafe());
                    assetStorageProvider.SetAttributeValue("Bucket", testContext.Properties["AWSBucket"].ToStringSafe());
                    assetStorageProvider.SetAttributeValue("Expiration", "525600");
                    assetStorageProvider.SetAttributeValue("RootFolder", testContext.Properties["UnitTestRootFolder"].ToStringSafe());
                    assetStorageProvider.SetAttributeValue("AWSProfileName", testContext.Properties["AWSProfileName"].ToStringSafe());
                    assetStorageProvider.SetAttributeValue("AWSAccessKey", accessKey);
                    assetStorageProvider.SetAttributeValue("AWSSecretKey", secretKey);

                    assetStorageProvider.SaveAttributeValues(rockContext);
                }
            }
            catch (System.Exception ex)
            {
                Assert.That.Inconclusive($"Unable to get the Amazon S3 Asset Storage Provider ({ex.Message}).");
            }
        }
Esempio n. 30
0
        /// <summary>
        /// Lists the files in folder. Asset.Key or Asset.Name is the folder.
        /// If Asset.Key is not provided then one is created using the RootFolder and Asset.Name.
        /// If Key and Name are not provided then list all files in the current RootFolder.
        /// If a key is provided it MUST use the full path, RootFolder and Name are not used.
        /// The last segment in the key is the folder name.
        /// </summary>
        /// <param name="assetStorageProvider"></param>
        /// <param name="asset"></param>
        /// <returns></returns>
        public override List <Asset> ListFilesInFolder(AssetStorageProvider assetStorageProvider, Asset asset)
        {
            string rootFolder = FixRootFolder(GetAttributeValue(assetStorageProvider, "RootFolder"));

            asset.Key = FixKey(asset, rootFolder);
            string physicalFolder = FileSystemComponentHttpContext.Server.MapPath(asset.Key);

            return(GetListOfObjects(assetStorageProvider, physicalFolder, SearchOption.TopDirectoryOnly, AssetType.File));
        }