コード例 #1
0
        private async Task <ImageMetadata> SaveData(AnalysisResultViewModel data)
        {
            var fileName = Path.GetFileName(data.ImageLocalPath);
            Func <Task <Stream> > imageCB;

            try
            {
                if (blobStorage == null || cosmosDb == null)
                {
                    BlobStorageHelper.ContainerName    = ConfigurationManager.AppSettings["ContainerName"];
                    BlobStorageHelper.ConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];
                    blobStorage = await BlobStorageHelper.BuildAsync();

                    CosmosDBHelper.AccessKey      = ConfigurationManager.AppSettings["CosmosDBKey"];
                    CosmosDBHelper.EndpointUri    = ConfigurationManager.AppSettings["CosmosDBEndpointURI"];
                    CosmosDBHelper.DatabaseName   = ConfigurationManager.AppSettings["DatabaseName"];
                    CosmosDBHelper.CollectionName = ConfigurationManager.AppSettings["CollectionName"];
                    cosmosDb = await CosmosDBHelper.BuildAsync();
                }
                var existing = await cosmosDb.FindDocumentByIdAsync <ImageMetadata>(fileName);

                var resized = ImageResizer.ResizeIfRequired(Server.MapPath(data.ImageLocalPath), 750);
                imageCB = async() => System.IO.File.OpenRead(resized.Item2);
                var imageBlob = await blobStorage.UploadImageAsync(imageCB, fileName);

                var metadata = new ImageMetadata(data.ImageLocalPath);
                metadata.AddInsights(data.Insights);
                metadata.BlobUri  = imageBlob.Uri;
                metadata.TaggedBy = User.Identity.Name;
                if (existing == null)
                {
                    metadata = (await cosmosDb.CreateDocumentIfNotExistsAsync(metadata, metadata.Id)).Item2;
                }
                else
                {
                    metadata = await cosmosDb.UpdateDocumentAsync(metadata, metadata.Id);
                }

                return(metadata);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }