Пример #1
0
 /// <summary>
 /// returns if there is no previous analysis for this particular user and a particular date
 /// </summary>
 /// <param name="model"></param>
 /// <param name="fcsPath"></param>
 /// <returns></returns>
 private bool ThereIsNoPreviousAnalysis(CloudFile model, string fcsPath)
 {
     return
         !_context.Analyses.Any(
             i =>
                 i.Patient.Id.Equals(model.Patient));
 }
Пример #2
0
        /// <summary>
        /// Uploads the current chunk to the storage
        /// </summary>
        /// <param name="model"></param>
        /// <param name="chunk"></param>
        /// <param name="id"></param>
        /// <param name="blobBlock"></param>
        /// <returns></returns>
        private async Task<JsonResult> UploadCurrentChunk(CloudFile model, byte[] chunk, int id, CloudBlockBlob blobBlock)
        {
            using (var chunkStream = new MemoryStream(chunk))
            {
                var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(
                        string.Format(CultureInfo.InvariantCulture, "{0:D4}", id)));
                try
                {
                    //model.BlockBlob.PutBlockAsync(
                    //    blockId,
                    //    chunkStream, null, null,
                    //    new BlobRequestOptions()
                    //    {
                    //        RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(10), 3)
                    //    },
                    //    null);

                    await blobBlock.PutBlockAsync(
                        blockId,
                        chunkStream, null, null,
                        new BlobRequestOptions()
                        {
                            RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(10), 3)
                        },
                        null);

                    return null;
                }
                catch (StorageException e)
                {
                    HttpContext.Session.Remove("CurrentFile");
                    model.IsUploadCompleted = true;
                    model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
                    return Json(new { error = true, isLastBlock = false, message = model.UploadStatusMessage });
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Sends every chunk of the .fcs file and sends an sms to the user
        /// </summary>
        /// <param name="model"></param>
        /// <param name="blockBlob"></param>
        /// <returns></returns>
        private async Task<ActionResult> CommitAllChunks(CloudFile model, CloudBlockBlob blockBlob)
        {
            model.IsUploadCompleted = true;

            var errorInOperation = false;

            try
            {
                var blockList = Enumerable.Range(1, (int)model.BlockCount).ToList().Select(rangeElement =>
                               Convert.ToBase64String(Encoding.UTF8.GetBytes(
                               string.Format(CultureInfo.InvariantCulture, "{0:D4}", rangeElement))));

                //model.BlockBlob.PutBlockListAsync(blockList);
                await blockBlob.PutBlockListAsync(blockList);

                var duration = DateTime.Now - model.StartTime;

                float fileSizeInKb = model.Size / 1024;

                var fileSizeMessage = fileSizeInKb > 1024 ?
                    string.Concat((fileSizeInKb / 1024).ToString(CultureInfo.CurrentCulture), " MB") :
                    string.Concat(fileSizeInKb.ToString(CultureInfo.CurrentCulture), " KB");

                var message = string.Format(CultureInfo.CurrentCulture,
                    "File uploaded successfully. {0} took {1} seconds to upload\n",
                    fileSizeMessage, duration.TotalSeconds);

                //Get the user
                var user = GetUser();
                //var fcsPath = user.LastName.ToLower() + "-" + user.FirstName.ToLower() + "-" + user.Id + "/" + model.FileName;
                var fcsPath = user.LastName.ToLower() + "-" + user.FirstName.ToLower() + "/" + model.FileName;

                var storedPatient = _context.Patients.First(x => x.Id == model.Patient);

                var analysis = new Analysis
                {
                    Date = DateTime.Now.Date,
                    FcsFilePath = fcsPath,
                    FcsUploadDate = DateTime.Now.Date.ToString("MM-dd-yyyy-hh-mm"),
                    ResultFilePath = "",
                    ResultDate = DateTime.Now.Date,
                    Delta = 0.00
                };

                storedPatient.Analyses.Add(analysis);
                user.Analyses.Add(analysis);
                _context.SaveChanges();

                if (!string.IsNullOrWhiteSpace(user.Phone))
                {
                    //send message to the user
                    _smsSender.SendSms(new AuthMessageSender.Message
                    {
                        Body = "Greetings" + "\nStatus on: " + model.OriginalFileName + "\n" + message
                    }, _sid, _authToken, _number, user.Phone);
                }

                model.UploadStatusMessage = message;
            }
            catch (StorageException e)
            {
                model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
                errorInOperation = true;
            }
            finally
            {
                HttpContext.Session.Remove("CurrentFile");
            }
            return Json(new
            {
                error = errorInOperation,
                isLastBlock = model.IsUploadCompleted,
                message = model.UploadStatusMessage
            });
        }
Пример #4
0
        public async Task<ActionResult> SetMetadata(int blocksCount, string fileName, long fileSize, string patient)
        {
   //         var storageAccount = CloudStorageAccount.Parse(_storageConnectionString);

            var patientCompleteName = patient.Split(' ');

            var firstName = patientCompleteName[0];
            var lastName = patientCompleteName[1];

            //container name will be lastname-name-id of the user. Everything in lowercase or Azure complains with a 400 error
            var user = GetUser();
            //var containerName = user.LastName + "-" + user.FirstName + "-" + user.Id;
            var containerName = user.LastName.ToLower() + "-" + user.FirstName.ToLower();
            //    var container = await GetContainer(storageAccount, containerName.ToLower());

            //get the patient
            var storedPatient = GetPatient(firstName, lastName);

            //blob exact name and location
            var blobName = lastName + "-" + firstName + "/" + DateTime.Now.ToString("MM-dd-yyyy-hh-mm") + ".fcs";

            //filename will be lastname-name-uploaddate.fcs of the patient
            var fileToUpload = new CloudFile()
            {
                OriginalFileName = fileName,
                Patient = storedPatient.Id,
                BlockCount = blocksCount,
                FileName = blobName.ToLower(),
                Size = fileSize,
                ContainerName = containerName,
                BlobName = blobName.ToLower(),
                StartTime = DateTime.Now,
                IsUploadCompleted = false,
                UploadStatusMessage = string.Empty
            };

            var fileByteArray = GetBytes(JsonConvert.SerializeObject(fileToUpload));

            HttpContext.Session.Set("CurrentFile", fileByteArray);

            return Json(true);
        }
Пример #5
0
 /// <summary>
 /// returns if there is no previous analysis for this particular user and a particular date
 /// </summary>
 /// <param name="model"></param>
 /// <param name="fcsPath"></param>
 /// <returns></returns>
 private bool ThereIsNoPreviousAnalysis(CloudFile model, string fcsPath)
 {
     return
         !_db.Analyses.Any(
             i =>
                 i.Patient.FirstName.Equals(model.Patient.FirstName) &&
                 i.Patient.LastName.Equals(model.Patient.LastName) && i.FcsFilePath.Equals(fcsPath));
 }
Пример #6
0
        /// <summary>
        /// Sends every chunk of the .fcs file and sends an sms to the user
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private ActionResult CommitAllChunks(CloudFile model)
        {
            model.IsUploadCompleted = true;

            var errorInOperation = false;

            try
            {
                var blockList = Enumerable.Range(1, (int)model.BlockCount).ToList().ConvertAll(rangeElement =>
                               Convert.ToBase64String(Encoding.UTF8.GetBytes(
                               string.Format(CultureInfo.InvariantCulture, "{0:D4}", rangeElement))));

                model.BlockBlob.PutBlockList(blockList);

                var duration = DateTime.Now - model.StartTime;

                float fileSizeInKb = model.Size / 1024;

                var fileSizeMessage = fileSizeInKb > 1024 ?
                    string.Concat((fileSizeInKb / 1024).ToString(CultureInfo.CurrentCulture), " MB") :
                    string.Concat(fileSizeInKb.ToString(CultureInfo.CurrentCulture), " KB");

                var message = string.Format(CultureInfo.CurrentCulture,
                    "File uploaded successfully. {0} took {1} seconds to upload\nYou'll receive another SMS when the results are completed",
                    fileSizeMessage, duration.TotalSeconds);

                //Get the user
                var user = GetUser();
                var fcsPath = user.LastName.ToLower() + "-" + user.FirstName.ToLower() + "-" + user.Id + "/" + model.FileName;
                //if the analysis did not exist before, add a new record to the db
                if (ThereIsNoPreviousAnalysis(model, fcsPath))
                {
                    var analysis = new Analysis
                    {
                        Date = DateTime.Now.Date,
                        FcsFilePath = fcsPath,
                        FcsUploadDate = DateTime.Now.ToString("MM-dd-yyyy"),
                        ResultFilePath = "",
                        ResultDate = DateTime.Now.Date,
                        Delta = 0.00
                    };

                    var storedPatient = GetPatient(model.Patient.FirstName, model.Patient.LastName);

                    storedPatient.Analyses.Add(analysis);
                    user.Analyses.Add(analysis);
                    _db.SaveChanges();
                }
                //otherwise, continue with the process and
                //notify the user about the success of the file upload
                SmsService.SendSms(new IdentityMessage
                {
                    Destination = user.Phone,
                    Body = Greeting + "\nStatus on: " + model.OriginalFileName + "\n" + message
                });

                model.UploadStatusMessage = message;
            }
            catch (StorageException e)
            {
                model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
                errorInOperation = true;
            }
            finally
            {
                Session.Remove("CurrentFile");
            }
            return Json(new
            {
                error = errorInOperation,
                isLastBlock = model.IsUploadCompleted,
                message = model.UploadStatusMessage
            });
        }
Пример #7
0
        public ActionResult SetMetadata(int blocksCount, string fileName, long fileSize, string patient)
        {
            var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

            var patientCompleteName = patient.Split(' ');

            var firstName = patientCompleteName[0];
            var lastName = patientCompleteName[1];

            //container name will be lastname-name-id of the user. Everything in lowercase or Azure complains with a 400 error
            var user = GetUser();
            var containerName = user.LastName + "-" + user.FirstName + "-" + user.Id;
            var container = GetContainer(storageAccount, containerName.ToLower());

            //get the patient
            var storedPatient = GetPatient(firstName, lastName);

            //blob exact name and location
            var blobName = lastName + "-" + firstName + "/" + DateTime.Now.ToString("MM-dd-yyyy") + ".fcs";

            //filename will be lastname-name-uploaddate.fcs of the patient
            var fileToUpload = new CloudFile()
            {
                OriginalFileName = fileName,
                Patient = storedPatient,
                BlockCount = blocksCount,
                FileName = blobName.ToLower(),
                Size = fileSize,
                BlockBlob = container.GetBlockBlobReference(blobName.ToLower()),
                StartTime = DateTime.Now,
                IsUploadCompleted = false,
                UploadStatusMessage = string.Empty
            };

            Session.Add("CurrentFile", fileToUpload);

            return Json(true);
        }