public void FileWrapper_ToMetaData_Returns_Properly_Hydrated()
        {
            var fileWrapper = new FileWrapper();
            fileWrapper.ApplicationName = "application";
            fileWrapper.FileName = "file.txt";
            fileWrapper.MimeType = "mimetype";
            fileWrapper.Payload = new byte []{1, 0, 1};

            var metaData = fileWrapper.ToMetaData();

            Assert.AreEqual(metaData.ApplicationName, fileWrapper.ApplicationName);
            Assert.AreEqual(metaData.FileName, fileWrapper.FileName);
            Assert.AreEqual(metaData.MimeType, fileWrapper.MimeType);
            Assert.AreEqual(metaData.Checksum, Checksummer.ComputeChecksum(fileWrapper.Payload));
        }
Ejemplo n.º 2
0
        public async Task<HttpResponseMessage> SaveFileAsync(FileWrapper file)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            // todo: consider rolling this into a validation attribute.  
            if (!string.IsNullOrEmpty(file.Sha256Checksum))
            {
                if (!Checksummer.ChecksumsMatch(file.Sha256Checksum, file.Payload))
                {
                    logger.Info(string.Format("Checksum mismatch for file with name: {0} from application: {1}", file.FileName, file.ApplicationName));
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The checksum submitted did not match the server computed SHA256 checksum.");
                }
            }

            try
            {
                var uniqueFileName = Guid.NewGuid() + "_" + file.FileName;
                var filePath = BuildFilePathAndAppendFileName(baseFileStoragePath, file.ApplicationName, uniqueFileName);
                await fileStorageProvider.StoreFile(filePath, file.Payload);

                var metaData = file.ToMetaData();
                metaData.StoragePath = filePath;
                var fileId = await metaDataRepository.SaveFileMetaDataAsync(metaData);

                var response = Request.CreateResponse(HttpStatusCode.Created, file);
                response.Headers.Location = new Uri(Url.Link("GetFileById", new { id = fileId}));

                return response;
            }
            catch (Exception e)
            {
                logger.Error(string.Format("error saving file with name: {0} for application: {1}", file.FileName, file.ApplicationName), e);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "File Storage API experienced an error.  This has been reported.  Please try again shortly.");
            }
        }