private string GetFileExtensionForDataFormat(byte[] fileBytes) { var imageFormat = ImageFormatDetector.GetImageFormat(fileBytes); if (imageFormat == ImageFormat.Unknown) { throw new NotImplementedException(); } return(imageFormat.GetFileExtensionForImageFormat().Replace("*", string.Empty)); }
private async Task <FileData> GetFileData(IDbConnection connection, IGuidGenerator guidGenerator, FileRepository fileRepository, string md5HashString, byte[] fileBytes, string fileName, string fileExtension) { if (!await fileRepository.HasFileWithSameHashAsync(connection, md5HashString)) { var fileId = guidGenerator.GenerateGuid(); return(new FileData(fileId, true, ImageFormatDetector.GetImageFormat(fileBytes) != ImageFormat.Unknown, fileBytes, md5HashString, fileBytes.LongLength, fileName, fileExtension)); } var fileDataFromStore = await fileRepository.GetFileByMd5HashAsync(connection, md5HashString); return(new FileData(fileDataFromStore.FileId, false, ImageFormatDetector.GetImageFormat(fileDataFromStore.Binary) != ImageFormat.Unknown, fileDataFromStore.Binary, fileDataFromStore.Md5Hash, fileDataFromStore.Size, fileDataFromStore.Name, fileDataFromStore.Extension)); }
private async Task <GenericDataContainer> FixMissingMetadata(string dataType, string id, byte[] binaryData) { // Assumption: Metadata doesn't exist var blob = binaryDataStorage.GetBlob(dataType, id); DateTime createdTimeUtc; DateTime submissionTimeUtc; if (await blob.ExistsAsync()) { createdTimeUtc = blob.CreatedTimestampUtc; submissionTimeUtc = blob.LastModifiedTimestampUtc; } else { var utcNow = DateTime.UtcNow; createdTimeUtc = utcNow; submissionTimeUtc = utcNow; } IId obj; switch (dataType) { case nameof(DataBlob): obj = new DataBlob(id, new byte[0]); break; case nameof(Image): var imageFormat = ImageFormatDetector.Detect(binaryData); var imageExtension = imageFormat != ImageFormatDetector.ImageFormat.Unknown ? imageFormat.GetFileExtension() : ".bin"; obj = new Image(id, new byte[0], imageExtension); break; default: throw new NotSupportedException($"Cannot fix missing metadata for ID '{id}' of data type '{dataType}'"); } var container = new GenericDataContainer("unknown", createdTimeUtc, "unknown", submissionTimeUtc, ApiVersion.Current, obj); await metadataStorage.StoreAsync(dataType, container, false); return(container); }