public async void LoadLatestDeviceTelemetryAsyncTest()
        {
            var year    = 2016;
            var month   = 7;
            var date    = 5;
            var minTime = new DateTime(year, month, date);

            var blobReader = new Mock <IBlobStorageReader>();
            var blobData   = "deviceid,temperature,humidity,externaltemperature,eventprocessedutctime,partitionid,eventenqueuedutctime,IoTHub" +
                             Environment.NewLine +
                             "test1,34.200411299709423,32.2233033525866,,2016-08-04T23:07:14.2549606Z,3," + minTime.ToString("o") + ",Record";
            var stream       = new MemoryStream(Encoding.UTF8.GetBytes(blobData));
            var blobContents = new BlobContents {
                Data = stream, LastModifiedTime = DateTime.UtcNow
            };
            var blobContentIterable = new List <BlobContents>();

            blobContentIterable.Add(blobContents);

            blobReader.Setup(x => x.GetEnumerator()).Returns(blobContentIterable.GetEnumerator());
            _blobStorageClientMock.Setup(x => x.GetReader(It.IsNotNull <string>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(blobReader.Object);
            var telemetryList = await deviceTelemetryRepository.LoadLatestDeviceTelemetryAsync("test1", null, minTime);

            Assert.NotNull(telemetryList);
            Assert.NotEmpty(telemetryList);
            Assert.Equal(telemetryList.First().DeviceId, "test1");
            Assert.Equal(telemetryList.First().Timestamp, minTime);
        }
Exemplo n.º 2
0
        public FileStreamResult StreamPrivate(Guid id)
        {
            string videoSecurity = TempData["VideoSecurity"] as string;

            if (!("VideoEdit" == videoSecurity || "MessageFromUrl" == videoSecurity || "MessagePreview" == videoSecurity))
            {
                throw new Exception("Video request is not authorized.");
            }

            Video vid = _videoRepository.GetById(id);

            BlobContainer container = AzureUtility.GetAzureContainer(vid.Path);

            BlobContents   contents = new BlobContents(new MemoryStream());
            BlobProperties blob     = container.GetBlob(vid.Path.Substring(vid.Path.IndexOf("/") + 1), contents, false);

            if (blob.ContentType == null)
            {
                throw new FormatException("No content type set for blob.");
            }

            Stream stream = contents.AsStream;

            stream.Seek(0, SeekOrigin.Begin);
            return(File(stream, blob.ContentType));
        }
Exemplo n.º 3
0
        internal static bool RefreshTextBlob(BlobContainer container, StringBlob stringBlob)
        {
            BlobContents   contents = new BlobContents(new MemoryStream());
            BlobProperties blob     = stringBlob.Blob;
            bool           modified = container.GetBlobIfModified(blob, contents, false);

            if (!modified)
            {
                return(false);
            }

            if (blob.ContentType == null)
            {
                throw new FormatException("No content type set for blob.");
            }

            ContentType blobMIMEType = new ContentType(blob.ContentType);

            if (!blobMIMEType.Equals(StringBlob.TextBlobMIMEType))
            {
                throw new FormatException("Not a text blob.");
            }

            stringBlob.Value = UnicodeEncoding.UTF8.GetString(contents.AsBytes());
            return(modified);
        }
        public async void LoadLatestDeviceTelemetrySummaryAsyncTest()
        {
            var year    = 2016;
            var month   = 7;
            var date    = 5;
            var minTime = new DateTime(year, month, date);

            var blobReader = new Mock <IBlobStorageReader>();
            var blobData   = "deviceid,averagespeed,minimumspeed,maximumspeed,timeframeminutes" + Environment.NewLine +
                             "test2,37.806204872115607,37.806204872115607,37.806204872115607,5";
            var stream       = new MemoryStream(Encoding.UTF8.GetBytes(blobData));
            var blobContents = new BlobContents {
                Data = stream, LastModifiedTime = DateTime.UtcNow
            };
            var blobContentIterable = new List <BlobContents>();

            blobContentIterable.Add(blobContents);

            blobReader.Setup(x => x.GetEnumerator()).Returns(blobContentIterable.GetEnumerator());
            _blobStorageClientMock.Setup(x => x.GetReader(It.IsNotNull <string>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(blobReader.Object);
            var telemetrySummaryList =
                await deviceTelemetryRepository.LoadLatestDeviceTelemetrySummaryAsync("test2", minTime);

            Assert.NotNull(telemetrySummaryList);
            Assert.Equal(telemetrySummaryList.DeviceId, "test2");
            Assert.Equal(telemetrySummaryList.AverageSpeed, 37.806204872115607);
            Assert.Equal(telemetrySummaryList.MinimumSpeed, 37.806204872115607);
            Assert.Equal(telemetrySummaryList.MaximumSpeed, 37.806204872115607);
            Assert.Equal(telemetrySummaryList.TimeFrameMinutes, 5);
        }
Exemplo n.º 5
0
        public async void LoadLatestAlertHistoryAsyncTest()
        {
            var year    = 2016;
            var month   = 7;
            var date    = 5;
            var value   = "10.0";
            var minTime = new DateTime(year, month, date);

            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() => await alertsRepository.LoadLatestAlertHistoryAsync(minTime, 0));

            var blobReader   = new Mock <IBlobStorageReader>();
            var blobData     = $"deviceid,reading,ruleoutput,time,{Environment.NewLine}Device123,{value},RuleOutput123,{minTime.ToString("o")}";
            var stream       = new MemoryStream(Encoding.UTF8.GetBytes(blobData));
            var blobContents = new BlobContents {
                Data = stream, LastModifiedTime = DateTime.UtcNow
            };
            var blobContentIterable = new List <BlobContents>();

            blobContentIterable.Add(blobContents);

            blobReader.Setup(x => x.GetEnumerator()).Returns(blobContentIterable.GetEnumerator());

            _blobStorageClientMock
            .Setup(x => x.GetReader(It.IsNotNull <string>(), It.IsAny <DateTime?>()))
            .ReturnsAsync(blobReader.Object);

            var alertsList = await alertsRepository.LoadLatestAlertHistoryAsync(minTime, 5);

            Assert.NotNull(alertsList);
            Assert.NotEmpty(alertsList);
            Assert.Equal(alertsList.First().Value, value);
            Assert.Equal(alertsList.First().Timestamp, minTime);
        }
Exemplo n.º 6
0
 internal static void DownloadToFile(BlobContainer container, string filepath, string blobName)
 {
     using (FileStream fs = File.Open(filepath, FileMode.Create))
     {
         BlobContents   contents = new BlobContents(fs);
         BlobProperties blob     = container.GetBlob(blobName, contents, true);
         Console.WriteLine("Downloaded blob {0} to file {1}", blob.Name, fs.Name);
         fs.Close();
     }
 }
Exemplo n.º 7
0
        private static async Task WriteToBlobAsync(ContestRequest request, IList <ContestResponse> contests)
        {
            BlobApi blobApi = await BlobApi.Instance;

            // Writes the blob to storage
            BlobContents contents = new BlobContents
            {
                BaseInputs = request,
                Contests   = contests
            };
            string jsonString = JsonConvert.SerializeObject(contents);
            string fileName   = $"{request.Mstpid}-{DateTime.Now:yyyy-MM}-{Guid.NewGuid()}";
            await blobApi.UploadToBlobAsync(jsonString, fileName);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets all the contest.
        /// </summary>
        /// <param name="tpid">If blank, return all contests</param>
        /// <returns></returns>
        public async Task <Tuple <IList <ContestResponse>, string> > GetAllContestTupleAsync(string tpid)
        {
            IList <BlobItem> blobs = await GetBlobItemsAsync(tpid);

            if (blobs.Count == 0)
            {
                throw new KeyNotFoundException($"Customer Code {tpid} was not found.");
            }

            string       blobName = blobs[0].Name;
            BlobContents contents = await GetBlobContentsAync <BlobContents>(blobName);

            return(new Tuple <IList <ContestResponse>, string>(contents.Contests, blobName));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets all the contest.
        /// </summary>
        /// <param name="tpid">If blank, return all contests</param>
        /// <returns></returns>
        public async Task <Tuple <IList <ContestResponse>, string> > GetAllContestTupleAsync(string tpid)
        {
            IList <BlobItem> blobs = await GetBlobItemsAsync(tpid);

            if (blobs.Count == 0)
            {
                return(null);
            }

            string       blobName = blobs[0].Name;
            BlobContents contents = await GetBlobContentsAync <BlobContents>(blobName);

            return(new Tuple <IList <ContestResponse>, string>(contents.Contests, blobName));
        }
Exemplo n.º 10
0
        public void SaveImage(string uid, string name, string description, string tags, string fileName, string contentType, byte[] data)
        {
            BlobProperties      properties = new BlobProperties(string.Format(CultureInfo.InvariantCulture, "image_{0}", uid));
            NameValueCollection metadata   = new NameValueCollection();

            metadata["Id"]          = uid;
            metadata["Filename"]    = fileName;
            metadata["ImageName"]   = String.IsNullOrEmpty(name) ? "unknown" : name;
            metadata["Description"] = String.IsNullOrEmpty(description) ? "unknown" : description;
            metadata["Tags"]        = String.IsNullOrEmpty(tags) ? "unknown" : tags;
            properties.Metadata     = metadata;
            properties.ContentType  = contentType;
            BlobContents imageBlob = new BlobContents(data);

            this.container.CreateBlob(properties, imageBlob, true);
        }
Exemplo n.º 11
0
        public void SaveImage(string id, string contentType, byte[] data)
        {
            BlobProperties      properties = new BlobProperties(string.Format(CultureInfo.InvariantCulture, "image_{0}", id));
            NameValueCollection metadata   = new NameValueCollection();

            metadata["Id"]          = id;
            metadata["Filename"]    = "userimage";
            metadata["ImageName"]   = "userimage";
            metadata["Description"] = "userimage";
            metadata["Tags"]        = "userimage";
            properties.Metadata     = metadata;
            properties.ContentType  = contentType;
            BlobContents imageBlob = new BlobContents(data);

            container.CreateBlob(properties, imageBlob, true);
        }
        public static bool PutBlob(WindowsAzureStorageHelper storageHelper, string containerName, string blobName, string fileName, byte[] fileContents, bool overwrite, NameValueCollection metadata)
        {
            BlobProperties blobProperties = new BlobProperties(blobName);

            blobProperties.ContentType = WindowsAzureStorageHelper.GetContentTypeFromExtension(Path.GetExtension(fileName));
            blobProperties.Metadata    = metadata;


            BlobContents blobContents = null;
            bool         ret          = false;

            blobContents = new BlobContents(fileContents);
            ret          = storageHelper.CreateBlob(containerName, blobProperties, blobContents, overwrite);


            return(ret);
        }
Exemplo n.º 13
0
        // *********************************************************
        // Create a new Blob in a Specific Blob Container
        // *********************************************************
        public void CreateBlob(string ContainerName, string FileName,
                               string BlobEndPoint, string Account, string SharedKey)
        {
            StorageAccountInfo AccountInfo =
                new StorageAccountInfo(new Uri(BlobEndPoint), null, Account, SharedKey);

            var blobStore = BlobStorage.Create(AccountInfo);

            var container = blobStore.GetBlobContainer(ContainerName);

            BlobProperties props = new BlobProperties(System.IO.Path.GetFileName(FileName));

            BlobContents blobContents =
                new BlobContents(new System.IO.FileStream(FileName, FileMode.Open));

            container.CreateBlob(props, blobContents, true);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Read a UTF-8 encoded string.
        /// </summary>
        /// <param name="blobName">Name of the blob</param>
        /// <returns>Contents of the blob.</returns>
        internal static StringBlob GetTextBlob(BlobContainer container, string blobName)
        {
            BlobContents   contents = new BlobContents(new MemoryStream());
            BlobProperties blob     = container.GetBlob(blobName, contents, false);

            if (blob.ContentType == null)
            {
                throw new FormatException("No content type set for blob.");
            }

            ContentType blobMIMEType = new ContentType(blob.ContentType);

            if (!blobMIMEType.Equals(StringBlob.TextBlobMIMEType))
            {
                throw new FormatException("Not a text blob.");
            }

            return(new StringBlob
            {
                Blob = blob,
                Value = Encoding.UTF8.GetString(contents.AsBytes())
            });
        }
 public static void SaveBlobLocally(string fileName, BlobProperties blobProperties, BlobContents blobContents)
 {
     if (blobContents != null && blobContents.AsBytes() != null && blobContents.AsBytes().Length > 0)
     {
         File.WriteAllBytes(fileName, blobContents.AsBytes());
     }//if
 }
 public static BlobProperties GetBlob(string containerName, string blobName, bool transferAsChunks, out BlobContents blobContents, WindowsAzureStorageHelper storageHelper)
 {
     blobContents = new BlobContents(new MemoryStream());
     return(storageHelper.GetBlob(containerName, blobName, blobContents, transferAsChunks));
 }
        public bool UpdateBlobIfNotModified(string containerName, BlobProperties cachedBlobProperties, BlobContents blobContents)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            return(container.UpdateBlobIfNotModified(cachedBlobProperties, blobContents));
        }
        public bool GetBlobIfModified(string containerName, string blobName, BlobProperties cachedBlobProperties, BlobContents returnedBlobContents, bool transferAsChunks)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            return(container.GetBlobIfModified(cachedBlobProperties, returnedBlobContents, transferAsChunks));
        }
        public BlobProperties GetBlob(string containerName, string blobName, BlobContents returnedBlobContents, bool transferAsChunks)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            return(container.GetBlob(blobName, returnedBlobContents, transferAsChunks));
        }
        public bool CreateBlob(string containerName, BlobProperties blobProperties, BlobContents blobContents, bool overwrite)
        {
            BlobContainer container = BlobStorageType.GetBlobContainer(containerName);

            return(container.CreateBlob(blobProperties, blobContents, overwrite));
        }
Exemplo n.º 21
0
        private string PostProcessVideo(UploadStatus status, decimal totalCount)
        {
            string resultMessage = String.Empty;
            string localFilePath = String.Empty;
            int    counter       = 0;

            //Setup the Azure Storage Container
            BlobContainer container = AzureUtility.GetAzureContainer(null);

            foreach (UploadedFile file in status.GetUploadedFiles())
            {
                try
                {
                    localFilePath = file.LocationInfo["fileName"].ToString();

                    //Save a skeleton record of the upload in the database
                    Video vid = _videoRepository.New();
                    _videoRepository.AddVideoToUser(vid, User.Identity.Name);
                    vid.Description         = file.FormValues["fileDescription"];
                    vid.StartProcessingDate = DateTime.Now;
                    vid.OriginalFileFormat  = file.ContentType;
                    vid.UploadSize          = file.ContentLength;
                    _videoRepository.Save();
                    Guid videoId = vid.Id;

                    string encodedFilePath = FFmpegEncoder.EncodeToWmv(localFilePath, file.ContentType);

                    if (!String.IsNullOrEmpty(encodedFilePath))
                    {
                        string fileNameOnly = encodedFilePath.Substring(encodedFilePath.LastIndexOf("Upload") + 7);

                        //TODO: Take a screenshot/Thumbnail of the video & upload it

                        BlobProperties properties = AzureUtility.CollectBlobMetadata(file, fileNameOnly, User.Identity.Name);

                        // Create the blob & Upload
                        long finalSize = 0;
                        using (FileStream uploadedFile = new FileStream(encodedFilePath, FileMode.Open, FileAccess.Read))
                        {
                            BlobContents fileBlob = new BlobContents(uploadedFile);
                            finalSize = fileBlob.AsStream.Length;
                            container.CreateBlob(properties, fileBlob, true);
                        }

                        //Create the database record for this video
                        vid = _videoRepository.GetById(videoId);
                        vid.CreationDate = DateTime.Now;
                        vid.FinalSize    = finalSize;
                        vid.Path         = String.Format("{0}/{1}", container.ContainerName, fileNameOnly);
                        _videoRepository.Save();

                        resultMessage = string.Format("Your video ({0}) is ready for you to use.", file.FormValues["fileDescription"]);

                        //Delete the local copy of the encoded file
                        System.IO.File.Delete(encodedFilePath);
                    }
                    else
                    {
                        resultMessage = "ERROR: video (" + file.FormValues["fileDescription"] + ") could not be converted to a recognizable video format.";
                    }

                    //Create a notification record so the user knows that processing is done for this video
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();
                }
                catch (Exception ex)
                {
                    resultMessage = string.Format("ERROR: we tried to process the video, {0}, but it did not finish.  You might want to try again.", file.FormValues["fileDescription"]);

                    //Create a notification record so the user knows that there was an error
                    Notification note = _notificationRepository.New();
                    _notificationRepository.AddNotificationToUser(note, User.Identity.Name);
                    note.UserNotified = false;
                    note.Message      = resultMessage;
                    note.CreationDate = DateTime.Now;
                    _notificationRepository.Save();

                    throw new Exception(resultMessage, ex);
                }
                finally
                {
                    //Delete the local copy of the original file
                    System.IO.File.Delete(localFilePath);

                    counter++;
                }
            }

            return(resultMessage);
        }