Ejemplo n.º 1
0
        public void TestCsvMetadataParsing()
        {
            List <AnimalImage> expectedValues = new List <AnimalImage>(new[]
            {
                new AnimalImage
                {
                    OriginalFileId         = "L-TJH15-V08A-0001",
                    OriginalImageId        = "IMAG0001",
                    FileFormat             = "JPG",
                    OriginalFolderId       = "L-TJH15-V08A",
                    CameraId               = "T0330",
                    LocationId             = "L-TJH15-V08A",
                    CameraInstallationDate = new DateTime(2017, 12, 01),
                    ShootingTime           = "19:00",
                    WorkingDays            = 1,
                    Category               = "工作人员",
                    IndependentProbeFirst  = "1",
                    Notes = "设置相机"
                },
                new AnimalImage
                {
                    OriginalFileId   = "L-TJH15-V08A-0002",
                    OriginalImageId  = "IMAG0002",
                    FileFormat       = "JPG",
                    OriginalFolderId = "L-TJH15-V08A",
                    CameraId         = "T0330",
                    LocationId       = "L-TJH15-V08A",
                    ShootingTime     = "19:00",
                    WorkingDays      = 1,
                    Category         = "工作人员"
                },
                new AnimalImage
                {
                    OriginalFileId   = "L-TJH15-V08A-0003",
                    OriginalImageId  = "IMAG0003",
                    FileFormat       = "JPG",
                    OriginalFolderId = "L-TJH15-V08A",
                    CameraId         = "T0330",
                    LocationId       = "L-TJH15-V08A",
                    ShootingTime     = "19:00",
                    WorkingDays      = 1,
                    Category         = "工作人员"
                },
                new AnimalImage
                {
                    OriginalFileId   = "L-TJH15-V08A-0004",
                    OriginalImageId  = "IMAG0004",
                    FileFormat       = "AVI",
                    OriginalFolderId = "L-TJH15-V08A",
                    CameraId         = "T0330",
                    LocationId       = "L-TJH15-V08A",
                    ShootingTime     = "19:00",
                    WorkingDays      = 1,
                    Category         = "工作人员"
                },
                new AnimalImage
                {
                    OriginalFileId        = "L-TJH15-V08A-0005",
                    OriginalImageId       = "IMAG0025",
                    FileFormat            = "JPG",
                    OriginalFolderId      = "L-TJH15-V08A",
                    CameraId              = "T0330",
                    LocationId            = "L-TJH15-V08A",
                    ShootingTime          = "2:12",
                    WorkingDays           = 2,
                    Category              = "兽类",
                    SpecicesName          = "猪獾",
                    AnimalQuantity        = 1,
                    IndependentProbeFirst = "1"
                }
            });

            List <AnimalImage> parsedData = AnimalImage.ReadFromCsv(ValidCsvMetadata);

            Assert.AreEqual(expectedValues.Count, parsedData.Count);

            for (int i = 0; i < expectedValues.Count; i++)
            {
                Assert.AreEqual(expectedValues[i].OriginalFileId, parsedData[i].OriginalFileId, $"Line {i}, field 'OriginalFileId'");
                Assert.AreEqual(expectedValues[i].OriginalImageId, parsedData[i].OriginalImageId, $"Line {i}, field 'OriginalImageId'");
                Assert.AreEqual(expectedValues[i].FileFormat, parsedData[i].FileFormat, $"Line {i}, field 'FileFormat'");
                Assert.AreEqual(expectedValues[i].OriginalFolderId, parsedData[i].OriginalFolderId, $"Line {i}, field 'OriginalFolderId'");
                Assert.AreEqual(expectedValues[i].CameraId, parsedData[i].CameraId, $"Line {i}, field 'CameraId'");
                Assert.AreEqual(expectedValues[i].LocationId, parsedData[i].LocationId, $"Line {i}, field 'LocationId'");
                Assert.AreEqual(expectedValues[i].CameraInstallationDate, parsedData[i].CameraInstallationDate, $"Line {i}, field 'CameraInstallationDate'");
                Assert.AreEqual(expectedValues[i].ShootingTime, parsedData[i].ShootingTime, $"Line {i}, field 'ShootingTime'");
                Assert.AreEqual(expectedValues[i].WorkingDays, parsedData[i].WorkingDays, $"Line {i}, field 'WorkingDays'");
                Assert.AreEqual(expectedValues[i].Category, parsedData[i].Category, $"Line {i}, field 'Category'");
                Assert.AreEqual(expectedValues[i].SpecicesName, parsedData[i].SpecicesName, $"Line {i}, field 'SpecicesName'");
                Assert.AreEqual(expectedValues[i].AnimalQuantity, parsedData[i].AnimalQuantity, $"Line {i}, field 'AnimalQuantity'");
                Assert.AreEqual(expectedValues[i].Sex, parsedData[i].Sex, $"Line {i}, field 'Sex'");
                Assert.AreEqual(expectedValues[i].IndependentProbeFirst, parsedData[i].IndependentProbeFirst, $"Line {i}, field 'IndependentProbeFirst'");
                Assert.AreEqual(expectedValues[i].Notes, parsedData[i].Notes, $"Line {i}, field 'Notes'");
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> UploadImageBatchAsync([FromBody] UploadBatchRequest value)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(value.BatchTag))
                {
                    throw new TncException()
                          {
                              StatusCode   = HttpStatusCode.BadRequest,
                              ErrorCode    = ErrorCode.ProperyCannotBeEmpty,
                              ErrorMessage = string.Format(ErrorMessages.PropertyCannotBeEmpty, "BatchTag")
                          };
                }

                if (string.IsNullOrWhiteSpace(value.CSVMetadata))
                {
                    throw new TncException()
                          {
                              StatusCode   = HttpStatusCode.BadRequest,
                              ErrorCode    = ErrorCode.ProperyCannotBeEmpty,
                              ErrorMessage = string.Format(ErrorMessages.PropertyCannotBeEmpty, "CSVMetadata")
                          };
                }

                List <AnimalImage> animalImages;
                try
                {
                    animalImages = AnimalImage.ReadFromCsv(value.CSVMetadata);
                }
                catch (Exception ex)
                {
                    throw new TncException()
                          {
                              StatusCode   = HttpStatusCode.BadRequest,
                              ErrorCode    = ErrorCode.BadCSVMetadata,
                              ErrorMessage = ex.Message
                          };
                }

                MetadataValidate(animalImages, a => string.IsNullOrWhiteSpace(a.OriginalFileId), "OriginalFileId is not present");
                MetadataValidate(animalImages, a => string.IsNullOrWhiteSpace(a.OriginalFolderId), "OriginalFolderId is not present");
                MetadataValidate(animalImages, a => string.IsNullOrWhiteSpace(a.OriginalImageId), "OriginalImageId is not present");
                MetadataValidate(animalImages, a => string.IsNullOrWhiteSpace(a.FileFormat), "FileFormat is not present");

                var operation = new OperationResult
                {
                    CreationTime = DateTime.UtcNow,
                    Id           = Guid.NewGuid().ToString(),
                    Status       = new OperationStatus {
                        Code = OperationStage.New
                    }
                };

                await this.AppConfiguration.CosmosDBClient.UpsertDocumentAsync(
                    this.AppConfiguration.OperationResultCollectionUri,
                    operation);

                Uri        originalUri = this.Request.RequestUri;
                UriBuilder uriBuilder  = new UriBuilder(originalUri.Scheme, originalUri.Host, originalUri.Port, $"/api/operationresults/{operation.Id}");

                this.Request.Properties[Constants.CustomResponseHeaders] = new Dictionary <string, string>()
                {
                    { Constants.LocationHeader, uriBuilder.Uri.AbsoluteUri },
                    { Constants.RetryAfterHeader, Constants.RetryIntervalInSeconds.ToString() }
                };

                return(this.Accepted());
            }
            catch (TncException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new TncException
                      {
                          ErrorCode    = "UnexpectedException",
                          ErrorMessage = ex.ToString()
                      };
            }
        }