/// <summary> /// Gets the file content from storage provider. /// </summary> /// <param name="binaryFile">The binary file.</param> private void GetFileContentFromStorageProvider(BinaryFile binaryFile) { Rock.Storage.ProviderComponent storageProvider = DetermineBinaryFileStorageProvider(binaryFile); if (storageProvider != null) { binaryFile.Data = binaryFile.Data ?? new BinaryFileData(); binaryFile.Data.Content = storageProvider.GetFileContent(binaryFile, HttpContext.Current); } }
/// <summary> /// Deletes the specified item. /// </summary> /// <param name="item">The item.</param> /// <param name="personId">The person identifier.</param> /// <returns></returns> public override bool Delete(BinaryFile item, int?personId) { // if we can determine the StorageProvider, use the provider to remove the file from the provider's external storage medium Rock.Storage.ProviderComponent storageProvider = DetermineBinaryFileStorageProvider(item); if (storageProvider != null) { storageProvider.RemoveFile(item, HttpContext.Current); } // delete the record from the database return(base.Delete(item, personId)); }
/// <summary> /// Determines the storage provider that was used the last time the file was saved /// </summary> /// <param name="item">The item.</param> /// <returns></returns> private Storage.ProviderComponent DetermineBinaryFileStorageProvider(BinaryFile item) { Rock.Storage.ProviderComponent storageProvider = null; if (item != null) { item.StorageEntityType = item.StorageEntityType ?? new EntityTypeService(this.RockContext).Get(item.StorageEntityTypeId ?? 0); if (item.StorageEntityType != null) { storageProvider = Rock.Storage.ProviderContainer.GetComponent(item.StorageEntityType.Name); } } return(storageProvider); }
/// <summary> /// Ends the get. /// </summary> /// <param name="asyncResult">The asynchronous result.</param> /// <param name="context">The context.</param> /// <returns></returns> public BinaryFile EndGet(IAsyncResult asyncResult, HttpContext context) { // restore the command from the context SqlCommand cmd = (SqlCommand)context.Items["cmd"]; using (SqlDataReader reader = cmd.EndExecuteReader(asyncResult)) { BinaryFile binaryFile = new BinaryFile(); // Columns must be read in Sequential Order (see stored procedure spBinaryFileGet) reader.Read(); binaryFile.Id = reader["Id"] as int? ?? 0; binaryFile.IsTemporary = (reader["IsTemporary"] as int?) == 1; binaryFile.IsSystem = (reader["IsSystem"] as int?) == 1; binaryFile.BinaryFileTypeId = reader["BinaryFileTypeId"] as int?; binaryFile.Url = reader["Url"] as string; binaryFile.FileName = reader["FileName"] as string; binaryFile.MimeType = reader["MimeType"] as string; binaryFile.LastModifiedDateTime = reader["LastModifiedDateTime"] as DateTime?; binaryFile.Description = reader["Description"] as string; int?storageEntityTypeId = reader["StorageEntityTypeId"] as int?; binaryFile.SetStorageEntityTypeId(storageEntityTypeId); var guid = reader["Guid"]; if (guid is Guid) { binaryFile.Guid = (Guid)guid; } string entityTypeName = reader["StorageEntityTypeName"] as string; binaryFile.Data = new BinaryFileData(); // read the fileContent from the database just in case it's stored in the database, otherwise, the Provider will get it var content = reader["Content"]; if (content != null) { binaryFile.Data.Content = content as byte[]; } Rock.Storage.ProviderComponent provider = Rock.Storage.ProviderContainer.GetComponent(entityTypeName); binaryFile.Data.Content = provider.GetFileContent(binaryFile, context); return(binaryFile); } }
/// <summary> /// Saves the specified <see cref="Rock.Model.BinaryFile"/>. /// </summary> /// <param name="item">A <see cref="Rock.Model.BinaryFile"/> to save.</param> /// <param name="personId">A <see cref="System.Int32"/> representing the PersonId of the <see cref="Rock.Model.Person"/> who is saving the BinaryFile..</param> /// <returns></returns> public override bool Save(BinaryFile item, int?personId) { item.LastModifiedDateTime = DateTime.Now; Rock.Storage.ProviderComponent storageProvider = DetermineBinaryFileStorageProvider(item); if (storageProvider != null) { //// if this file is getting replaced, and we can determine the StorageProvider, use the provider to get and remove the file from the provider's //// external storage medium before we save it again. This especially important in cases where the provider for this filetype has changed //// since it was last saved // first get the FileContent from the old/current fileprovider in case we need to save it somewhere else item.Data = item.Data ?? new BinaryFileData(); item.Data.Content = storageProvider.GetFileContent(item, HttpContext.Current); // now, remove it from the old/current fileprovider storageProvider.RemoveFile(item, HttpContext.Current); } // when a file is saved (unless it is getting Deleted/Saved), it should use the StoredEntityType that is associated with the BinaryFileType if (item.BinaryFileType != null) { // make sure that it updated to use the same storage as specified by the BinaryFileType if (item.StorageEntityTypeId != item.BinaryFileType.StorageEntityTypeId) { item.SetStorageEntityTypeId(item.BinaryFileType.StorageEntityTypeId); storageProvider = DetermineBinaryFileStorageProvider(item); } } if (storageProvider != null) { // save the file to the provider's new storage medium storageProvider.SaveFile(item, HttpContext.Current); } return(base.Save(item, personId)); }