Exemplo n.º 1
0
        public IHttpActionResult Delete(string id)
        {
            Log.Information($"Deleting file {id}.");
            var descriptor = _repository.LoadObject <FileDescriptor>(id);

            if (descriptor == null)
            {
                Log.Information($"File {id} not found.");
                return(Ok());                // DELETE is indempotent
            }

            _repository.Delete(descriptor.ProcessingInfoId);
            _repository.DeleteStream(descriptor.BlobId);
            _repository.Delete(id);

            Log.Information($"File {id} deleted.");
            return(Ok());
        }
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);
                }
            }
        }