Exemplo n.º 1
0
        private HttpResponseMessage HttpResponseWithStream(string fileId, string blobId, string fileName)
        {
            var mediaType = MimeTypeMap.GetMimeType(Path.GetExtension(fileName));

            var response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new PushStreamContent((stream, httpContent, transportContext) =>
            {
                try
                {
                    Log.Information($"Sending file {fileId} finised success.");
                    _repository.LoadStream(blobId, stream);
                }
                catch (HttpException ex)
                {
                    Log.Warning(ex, $"Sending file {fileId} finised with error.");

                    // TODO: Magic number use - need to create a constant for this error
                    if (ex.ErrorCode == -2147023667)                     // The remote host closed the connection.
                    {
                        return;
                    }
                }
                finally
                {
                    // Close output stream as we are done
                    stream.Close();
                }
            });
            response.Content.Headers.ContentType        = new MediaTypeHeaderValue(mediaType);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName
            };

            return(response);
        }
Exemplo n.º 2
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);
                }
            }
        }