Exemplo n.º 1
0
        /// <summary>
        /// Copies a file stored in Windows Azure Storage to another blob.
        /// </summary>
        /// <param name="sourceContainerName">Name of the container where the source blob resides.</param>
        /// <param name="sourceBlobName">Name of the blob to copy data from.</param>
        /// <param name="destinationContainerName">Name of the container where the destination blob will be.</param>
        /// <param name="destinationBlobName">Name of the blob to copy data to.</param>
        public void CopyStoredFile(
            [NotNull] string sourceContainerName,
            [NotNull] string sourceBlobName,
            [NotNull] string destinationContainerName,
            [NotNull] string destinationBlobName)
        {
            if (sourceContainerName == null)
            {
                throw new ArgumentNullException("sourceContainerName");
            }
            if (sourceBlobName == null)
            {
                throw new ArgumentNullException("sourceBlobName");
            }
            if (destinationContainerName == null)
            {
                throw new ArgumentNullException("destinationContainerName");
            }
            if (destinationBlobName == null)
            {
                throw new ArgumentNullException("destinationBlobName");
            }

            var srcLocation = new BlobLocation(sourceContainerName, sourceBlobName);
            var dstLocation = new BlobLocation(destinationContainerName, destinationBlobName);

            InternalCopyFile(srcLocation, dstLocation);
        }
        /// <summary>
        /// Copies a file stored in Windows Azure Storage to another blob.
        /// </summary>
        /// <param name="sourceContainerName">Name of the container where the source blob resides.</param>
        /// <param name="sourceBlobName">Name of the blob to copy data from.</param>
        /// <param name="destinationContainerName">Name of the container where the destination blob will be.</param>
        /// <param name="destinationBlobName">Name of the blob to copy data to.</param>
        public void CopyStoredFile(
            [NotNull] string sourceContainerName,
            [NotNull] string sourceBlobName,
            [NotNull] string destinationContainerName,
            [NotNull] string destinationBlobName)
        {
            if (sourceContainerName == null) throw new ArgumentNullException("sourceContainerName");
            if (sourceBlobName == null) throw new ArgumentNullException("sourceBlobName");
            if (destinationContainerName == null) throw new ArgumentNullException("destinationContainerName");
            if (destinationBlobName == null) throw new ArgumentNullException("destinationBlobName");

            var srcLocation = new BlobLocation(sourceContainerName, sourceBlobName);
            var dstLocation = new BlobLocation(destinationContainerName, destinationBlobName);

            InternalCopyFile(srcLocation, dstLocation);
        }
Exemplo n.º 3
0
        public bool InternalCopyFile(BlobLocation fileLocation, BlobLocation destinationFileLocation)
        {
            var sourcePath      = Path.Combine(DebugConfig.LocalStoragePath, fileLocation.FullName);
            var destinationPath = Path.Combine(DebugConfig.LocalStoragePath, destinationFileLocation.FullName);

            var destDir = Path.GetDirectoryName(destinationPath);

            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

            var result = File.Exists(sourcePath);

            if (result)
            {
                File.Copy(sourcePath, destinationPath);
            }

            return(result);
        }
 /// <summary>
 /// Copies a file stored in Windows Azure Storage to another blob.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="sourceLocation"> Source location of the file to copy data from. </param>
 /// <param name="destinationLocation"> Destination location to copy the file data to. </param>
 public static void CopyStoredFile(this IBlobStorageManager @this, BlobLocation sourceLocation, BlobLocation destinationLocation)
 {
     @this.CopyStoredFile(sourceLocation.ContainerName, sourceLocation.BlobName, destinationLocation.ContainerName, destinationLocation.BlobName);
 }
 /// <summary>
 /// Uploads a file to the Windows Azure Storage.
 /// </summary>
 /// <param name="stream">Stream containing the data to be saved into the file.</param>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to upload to.</param>
 public static void UploadFileToStorage(this IBlobStorageManager @this, [NotNull] Stream stream, BlobLocation location)
 {
     @this.UploadFileToStorage(stream, location.ContainerName, location.BlobName);
 }
 /// <summary>
 /// Downloads a file from the Windows Azure Storage.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to download.</param>
 /// <returns>Returns a valid stream that can be used to read file data, or null if the file does not exist.</returns>
 public static Stream DownloadFileFromStorage(this IBlobStorageManager @this, BlobLocation location)
 {
     return(@this.DownloadFileFromStorage(location.ContainerName, location.BlobName));
 }
 /// <summary>
 /// Deletes a file from the Window Azure Storage.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to delete.</param>
 public static void DeleteFileFromStorage(this IBlobStorageManager @this, BlobLocation location)
 {
     @this.DeleteFileFromStorage(location.ContainerName, location.BlobName);
 }
 /// <summary>
 /// Gets the length of a file if it exists.
 /// Null if the file does not exist.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to get the length of.</param>
 /// <returns>Returns the length of the file if it exists, or null if it does not exist.</returns>
 public static long?GetFileLength(this IBlobStorageManager @this, BlobLocation location)
 {
     return(@this.GetFileLength(location.ContainerName, location.BlobName));
 }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a thumbnail image of a file in the storage.
        /// </summary>
        /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param>
        /// <param name="maxWidth">Maximum width of the thumbnail image.</param>
        /// <param name="maxHeight">Maximum height of the thumbnail image.</param>
        /// <param name="sourceFullStorageFileName">Name of the source image file.</param>
        /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param>
        /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param>
        /// <param name="storage">Storage service used to get file data.</param>
        /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param>
        /// <returns>Returns the result of the thumbnail creation process.</returns>
        public static CreateThumbResult TryGetOrCreateThumb(
            int originalMetadataId,
            int maxWidth,
            int maxHeight,
            string sourceFullStorageFileName,
            string thumbFullStorageFileName,
            bool loadFromCache,
            IBlobStorageManager storage,
            IFileMetadataProvider fileMetadataProvider)
        {
            var srcBlobLocation = new BlobLocation(sourceFullStorageFileName);
            var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName);

            if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName))
            {
                var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation);
                if (thumbStream != null)
                {
                    using (thumbStream)
                    using (var stream = new MemoryStream((int)thumbStream.Length))
                    {
                        thumbStream.CopyTo(stream);
                        {
                            return new CreateThumbResult(
                                CreateThumbStatus.Ok,
                                stream.ToArray(),
                                MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName)));
                        }
                    }
                }
            }

            if (!StringHelper.IsImageFileName(sourceFullStorageFileName))
                return new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null);

            var srcStream = storage.DownloadFileFromStorage(srcBlobLocation);

            if (srcStream == null)
                return new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null);

            string contentType;
            byte[] array;
            using (srcStream)
            using (var srcImage = Image.FromStream(srcStream))
            {
                var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0;
                if (imageSizeMegabytes > 40.0)
                    return new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null);

                using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false))
                using (var newStream = new MemoryStream())
                {
                    if (newImage == null)
                    {
                        srcStream.Position = 0;
                        srcStream.CopyTo(newStream);
                        contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName));
                    }
                    else
                    {
                        var imageFormat = (newImage.Width * newImage.Height > 10000)
                            ? ImageFormat.Jpeg
                            : ImageFormat.Png;

                        contentType = (newImage.Width * newImage.Height > 10000)
                            ? "image/jpeg"
                            : "image/png";

                        newImage.Save(newStream, imageFormat);
                    }

                    array = newStream.ToArray();

                    if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName))
                    {
                        // saving thumbnail image file metadata
                        var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight);
                        var metadata = fileMetadataProvider.CreateRelated(
                            originalMetadataId,
                            relationType,
                            thumbBlobLocation.ContainerName,
                            thumbBlobLocation.FileName,
                            thumbBlobLocation.BlobName,
                            null);

                        fileMetadataProvider.SaveChanges();

                        storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation);
                    }
                }
            }

            return new CreateThumbResult(CreateThumbStatus.Ok, array, contentType);
        }
 /// <summary>
 /// Copies a file stored in Windows Azure Storage to another blob.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="sourceLocation"> Source location of the file to copy data from. </param>
 /// <param name="destinationLocation"> Destination location to copy the file data to. </param>
 public static void CopyStoredFile(this IBlobStorageManager @this, BlobLocation sourceLocation, BlobLocation destinationLocation)
 {
     @this.CopyStoredFile(sourceLocation.ContainerName, sourceLocation.BlobName, destinationLocation.ContainerName, destinationLocation.BlobName);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Uploads a partial file to the server.
        /// </summary>
        /// <param name="fileName"> The file name. </param>
        /// <param name="prefix"> The prefix of the fields to be placed in the HTML. </param>
        /// <param name="location"> The storage location for the temporary file. </param>
        /// <returns> The <see cref="ActionResult"/> containing information about execution of the upload. </returns>
        /// <exception cref="HttpRequestValidationException"> When more than one file is uploaded, or when the file is null. </exception>
        private ActionResult UploadPartialFile(string fileName, string prefix, string location, string tag)
        {
            // todo: partial file upload is not yet supported

            if (this.Request.Files.Count != 1)
                throw new HttpRequestValidationException(
                    "Attempt to upload chunked file containing more than one fragment per request");

            var httpPostedFileBase = this.Request.Files[0];
            if (httpPostedFileBase == null)
                throw new HttpRequestValidationException("Posted file is null.");

            var inputStream = httpPostedFileBase.InputStream;

            fileName = Path.GetFileName(fileName);
            Debug.Assert(fileName != null, "fileName != null");
            var fullLocation = new BlobLocation(Path.Combine(location, fileName));

            // todo: Must append to blob block
            // this.storage.CreateOrAppendToFile(fullPath, inputStream);

            var fileLength = (long)this.storage.GetFileLength(fullLocation.ContainerName, fullLocation.BlobName);

            // todo: must create a valid file metadata, or retrieve an already existing one from the database
            int id = 0;
            var fileStatus = new FilesStatus(id, fileName, fileLength, prefix);

            // when doing partial upload of a file, no thumb-image will be generated
            // also the file wont display in the gallery (because no url will be provided)
            if (StringHelper.IsDocumentFileName(fileName))
            {
                fileStatus.IconClass = "document-file-icon";
            }
            else
            {
                fileStatus.IconClass = "generic-file-icon";
            }

            fileStatus.UrlLarge = null;

            // cannot download when it is a partial uploaded file
            fileStatus.UrlFull = null;

            return this.JsonIframeSafe(new { files = new[] { fileStatus } });
        }
 /// <summary>
 /// Uploads a file to the Windows Azure Storage.
 /// </summary>
 /// <param name="stream">Stream containing the data to be saved into the file.</param>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to upload to.</param>
 public static void UploadFileToStorage(this IBlobStorageManager @this, [NotNull] Stream stream, BlobLocation location)
 {
     @this.UploadFileToStorage(stream, location.ContainerName, location.BlobName);
 }
 /// <summary>
 /// Gets the length of a file if it exists.
 /// Null if the file does not exist.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to get the length of.</param>
 /// <returns>Returns the length of the file if it exists, or null if it does not exist.</returns>
 public static long? GetFileLength(this IBlobStorageManager @this, BlobLocation location)
 {
     return @this.GetFileLength(location.ContainerName, location.BlobName);
 }
 /// <summary>
 /// Downloads a file from the Windows Azure Storage.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to download.</param>
 /// <returns>Returns a valid stream that can be used to read file data, or null if the file does not exist.</returns>
 public static Stream DownloadFileFromStorage(this IBlobStorageManager @this, BlobLocation location)
 {
     return @this.DownloadFileFromStorage(location.ContainerName, location.BlobName);
 }
 /// <summary>
 /// Deletes a file from the Window Azure Storage.
 /// </summary>
 /// <param name="this">Blob storage manager to use.</param>
 /// <param name="location">Location of the blob to delete.</param>
 public static void DeleteFileFromStorage(this IBlobStorageManager @this, BlobLocation location)
 {
     @this.DeleteFileFromStorage(location.ContainerName, location.BlobName);
 }
Exemplo n.º 16
0
        public bool InternalCopyFile(BlobLocation fileLocation, BlobLocation destinationFileLocation)
        {
            var sourcePath = Path.Combine(DebugConfig.LocalStoragePath, fileLocation.FullName);
            var destinationPath = Path.Combine(DebugConfig.LocalStoragePath, destinationFileLocation.FullName);

            var destDir = Path.GetDirectoryName(destinationPath);
            if (!Directory.Exists(destDir))
                Directory.CreateDirectory(destDir);

            var result = File.Exists(sourcePath);
            if (result)
                File.Copy(sourcePath, destinationPath);

            return result;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a thumbnail image of a file in the storage.
        /// </summary>
        /// <param name="originalMetadataId">Metadata entry ID for the original image file.</param>
        /// <param name="maxWidth">Maximum width of the thumbnail image.</param>
        /// <param name="maxHeight">Maximum height of the thumbnail image.</param>
        /// <param name="sourceFullStorageFileName">Name of the source image file.</param>
        /// <param name="thumbFullStorageFileName">Name of the thumbnail image cache file.</param>
        /// <param name="loadFromCache">Whether to use a cached thumbnail or not.</param>
        /// <param name="storage">Storage service used to get file data.</param>
        /// <param name="fileMetadataProvider">File metadata provider used to create thumbnail image metadata.</param>
        /// <returns>Returns the result of the thumbnail creation process.</returns>
        public static CreateThumbResult TryGetOrCreateThumb(
            int originalMetadataId,
            int maxWidth,
            int maxHeight,
            string sourceFullStorageFileName,
            string thumbFullStorageFileName,
            bool loadFromCache,
            IBlobStorageManager storage,
            IFileMetadataProvider fileMetadataProvider)
        {
            var srcBlobLocation   = new BlobLocation(sourceFullStorageFileName);
            var thumbBlobLocation = new BlobLocation(thumbFullStorageFileName);

            if (loadFromCache && !string.IsNullOrEmpty(thumbFullStorageFileName))
            {
                var thumbStream = storage.DownloadFileFromStorage(thumbBlobLocation);
                if (thumbStream != null)
                {
                    using (thumbStream)
                        using (var stream = new MemoryStream((int)thumbStream.Length))
                        {
                            thumbStream.CopyTo(stream);
                            {
                                return(new CreateThumbResult(
                                           CreateThumbStatus.Ok,
                                           stream.ToArray(),
                                           MimeTypesHelper.GetContentType(Path.GetExtension(thumbFullStorageFileName))));
                            }
                        }
                }
            }

            if (!StringHelper.IsImageFileName(sourceFullStorageFileName))
            {
                return(new CreateThumbResult(CreateThumbStatus.SourceIsNotImage, null, null));
            }

            var srcStream = storage.DownloadFileFromStorage(srcBlobLocation);

            if (srcStream == null)
            {
                return(new CreateThumbResult(CreateThumbStatus.SourceFileNotFound, null, null));
            }

            string contentType;

            byte[] array;
            using (srcStream)
                using (var srcImage = Image.FromStream(srcStream))
                {
                    var imageSizeMegabytes = srcImage.Width * srcImage.Height * 4 / 1024000.0;
                    if (imageSizeMegabytes > 40.0)
                    {
                        return(new CreateThumbResult(CreateThumbStatus.SourceImageTooLarge, null, null));
                    }

                    using (var newImage = ResizeImage(srcImage, maxWidth, maxHeight, keepAspect: true, canGrow: false))
                        using (var newStream = new MemoryStream())
                        {
                            if (newImage == null)
                            {
                                srcStream.Position = 0;
                                srcStream.CopyTo(newStream);
                                contentType = MimeTypesHelper.GetContentType(Path.GetExtension(sourceFullStorageFileName));
                            }
                            else
                            {
                                var imageFormat = (newImage.Width * newImage.Height > 10000)
                            ? ImageFormat.Jpeg
                            : ImageFormat.Png;

                                contentType = (newImage.Width * newImage.Height > 10000)
                            ? "image/jpeg"
                            : "image/png";

                                newImage.Save(newStream, imageFormat);
                            }

                            array = newStream.ToArray();

                            if (loadFromCache && newImage != null && !string.IsNullOrEmpty(thumbFullStorageFileName))
                            {
                                // saving thumbnail image file metadata
                                var relationType = string.Format("thumb-{0}x{1}", maxWidth, maxHeight);
                                var metadata     = fileMetadataProvider.CreateRelated(
                                    originalMetadataId,
                                    relationType,
                                    thumbBlobLocation.ContainerName,
                                    thumbBlobLocation.FileName,
                                    thumbBlobLocation.BlobName,
                                    null);

                                fileMetadataProvider.SaveChanges();

                                storage.UploadFileToStorage(new MemoryStream(array), thumbBlobLocation);
                            }
                        }
                }

            return(new CreateThumbResult(CreateThumbStatus.Ok, array, contentType));
        }
Exemplo n.º 18
0
        /// <summary>
        /// Sets a person picture as the specified tempFileName.
        /// The tempFileName will be COPIED from the "temp" Azure Storage container name
        /// tempFileName can be deleted afterwards
        /// </summary>
        /// <param name="personId"></param>
        /// <param name="tempFileName"></param>
        /// <returns></returns>
        public JsonResult TransferPersonPictureFromTempContainer(int personId, string tempFileName)
        {
            try
            {
                var person = this.db.Persons.First(p => p.Id == personId);
                var profilePictureBlobName = "profile_picture_" + personId;

                var sourceLocation = new BlobLocation(Constants.AZURE_STORAGE_TEMP_FILES_CONTAINER_NAME, tempFileName);
                var destinationLocation = new BlobLocation(Constants.PERSON_PROFILE_PICTURE_CONTAINER_NAME, profilePictureBlobName);

                // download temp file
                var storageManager = new WindowsAzureBlobStorageManager();
                //var tempFile = storageManager.DownloadFileFromStorage(sourceLocation);

                // if person has a profile picture already, delete it
                if (person.PictureBlobName != null)
                    storageManager.DeleteFileFromStorage(destinationLocation);

                storageManager.CopyStoredFile(sourceLocation, destinationLocation);

                // upload downloaded temp file to person profile
                //storageManager.UploadFileToStorage(tempFile, Constants.PERSON_PROFILE_PICTURE_CONTAINER_NAME, profilePictureBlobName);
                person.PictureBlobName = profilePictureBlobName;

                // delete temp file
                storageManager.DeleteFileFromStorage(sourceLocation);

                // this controller shouldn't know about patients but.. it's the easiest way to do this now
                if (person.Patients.Any())
                    person.Patients.First().IsBackedUp = false;

                this.db.SaveChanges();

                return this.Json(
                    new
                    {
                        success = true
                    }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.Json(
                    new
                    {
                        success = false,
                        message = ex.Message
                    }, JsonRequestBehavior.AllowGet);
            }
        }