Ejemplo n.º 1
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);
            }
        }