Beispiel #1
0
        public void ProcessImage(SourceFileUploadedMessage message)
        {
            string extension = Path.GetExtension(message.SourceFileName);

            if (!FileRasterizer.Supports(extension))
            {
                var error = $"Unsupported file type {extension}";

                new ProcessingInfo
                {
                    FileDescriptorId = message.ImageDescriptorId,
                    ProcessingError  = error
                }.SaveTo(_repository);

                Log.Information(error);
                return;
            }

            var format        = _options.ImageFormat.ParseImageFormat();
            var imageBlobId   = Guid.NewGuid().Encode();
            var imageFileName = $"{imageBlobId}.{format}";
            var rasterizer    = new FileRasterizer();

            var sourceTempFilePath = Path.Combine(Environment.CurrentDirectory, Guid.NewGuid().Encode());

            try
            {
                using (var sourceStream = File.Create(sourceTempFilePath))
                {
                    Log.Information($"Loading source {message.SourceDescriptorId}");
                    _repository.LoadStream(message.SourceBlobId, sourceStream);

                    Log.Information($"Rasterizing source {message.SourceDescriptorId}");
                    sourceStream.Position = 0;
                    var image = rasterizer.Rasterize(sourceStream, extension);
                    image = image.Scale(message.ImageSize.Width, message.ImageSize.Height);

                    Log.Information($"Saving image file {message.ImageDescriptorId} as {format}");

                    using (var imageStream = new MemoryStream())
                    {
                        var imageData = image.Convert(format);
                        imageStream.Write(imageData, 0, imageData.Length);

                        _repository.SaveStream(imageBlobId, imageStream);

                        new FileDescriptor
                        {
                            DescriptorId = message.ImageDescriptorId,
                            FileName     = imageFileName,
                            BlobId       = imageBlobId
                        }.SaveTo(_repository);

                        _repository.SetExpiration(message.ImageDescriptorId, message.SourceExpiry);
                        _repository.SetStreamExpiration(imageBlobId, message.SourceExpiry);

                        _repository.Delete(message.ProcessingInfoId);
                    }

                    Log.Information($"Image {message.ImageDescriptorId} processed");
                }
            }
            catch (Exception ex)
            {
                var error = $"Processing exception for source {message.SourceDescriptorId}: {ex.ToString()}";

                new ProcessingInfo
                {
                    FileDescriptorId = message.ImageDescriptorId,
                    ProcessingError  = error
                }.SaveTo(_repository);

                Log.Information(error);
            }
            finally
            {
                if (File.Exists(sourceTempFilePath))
                {
                    File.Delete(sourceTempFilePath);
                }
            }
        }
Beispiel #2
0
        private UploadedFileInfo SaveFile(MultipartFileData file)
        {
            var fileName = file.Headers.ContentDisposition.FileName ?? file.Headers.ContentDisposition.Name;

            fileName = fileName?.Trim('"');
            var fileInfo = new UploadedFileInfo {
                FileName = fileName
            };

            var sourceDescriptorId = Guid.NewGuid().Encode();
            var imageDescriptorId  = Guid.NewGuid().Encode();
            var processingInfoId   = Guid.NewGuid().Encode();

            try
            {
                Log.Information($"Uploading file {sourceDescriptorId}");
                var sourceBlobId = UploadFileData(file.LocalFileName);
                var fileExpiry   = GetFileExpiry(file);

                // save source descriptor
                Log.Information($"Saving source desctiptor {sourceDescriptorId}");
                new FileDescriptor
                {
                    DescriptorId = sourceDescriptorId,
                    FileName     = fileName,
                    BlobId       = sourceBlobId,
                }.SaveTo(_repository);
                _repository.SetExpiration(sourceDescriptorId, fileExpiry);
                _repository.SetStreamExpiration(sourceBlobId, fileExpiry);

                // save image descriptor
                Log.Information($"Saving image desctiptor {imageDescriptorId}");
                new FileDescriptor
                {
                    DescriptorId     = imageDescriptorId,
                    ProcessingInfoId = processingInfoId,
                }.SaveTo(_repository);
                _repository.SetExpiration(imageDescriptorId, fileExpiry);

                // save processing info
                Log.Information($"Saving processing info {processingInfoId}");
                new ProcessingInfo()
                {
                    ProcessingInfoId = processingInfoId,
                    FileDescriptorId = imageDescriptorId,
                }.SaveTo(_repository);
                _repository.SetExpiration(processingInfoId, fileExpiry);

                // publish message for processing
                Log.Information($"Publishing processing message for source {sourceDescriptorId}");
                _bus.Publish(new SourceFileUploadedMessage
                {
                    SourceDescriptorId = sourceDescriptorId,
                    SourceBlobId       = sourceBlobId,
                    SourceFileName     = fileName,
                    ImageDescriptorId  = imageDescriptorId,
                    ProcessingInfoId   = processingInfoId,
                    SourceExpiry       = fileExpiry,
                    ImageSize          = GetImageSize(file)
                });

                fileInfo.SourceId = sourceDescriptorId;
                fileInfo.ImageId  = imageDescriptorId;
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Saving file {sourceDescriptorId} with error.");
                fileInfo.Error = ex.Message;
            }

            File.Delete(file.LocalFileName);
            return(fileInfo);
        }