Example #1
0
        public async Task Run(string fileId)
        {
            EnsureFolderExists(_workPath);
            EnsureFolderExists(_outputPath);

            var shallowStorageObject = await _provider.GetObjectMetadata(
                Shared.Constants.Buckets.Original,
                fileId);

            if (shallowStorageObject.Metadata.ContainsKey(Shared.Constants.Metadata.FileType) &&
                shallowStorageObject.Metadata.ContainsKey(Shared.Constants.Metadata.ConvertTo))
            {
                var extension = shallowStorageObject.Metadata[Shared.Constants.Metadata.FileType];
                var convertTo = shallowStorageObject.Metadata[Shared.Constants.Metadata.ConvertTo];

                var fileName = fileId + "." + extension;
                var fullPath = Path.Combine(_workPath, fileName);

                using (var file = File.OpenWrite(fullPath))
                {
                    var storageFile = await _provider.GetObject(Shared.Constants.Buckets.Original, fileId);

                    using (var stream = storageFile.Data)
                    {
                        await stream.CopyToAsync(file);
                    }
                }

                var result = Converter.Convert(fullPath, convertTo, _workPath);
                if (result.Successful)
                {
                    var resultPath = await CopyFile(result.OutputFile, _outputPath);

                    var info = new FileInfo(resultPath);

                    var contentType = KitchenSink.GetContentTypeFromExtension(info.Extension);

                    using (var file = File.OpenRead(resultPath))
                    {
                        await _provider.UploadObject(
                            file,
                            Shared.Constants.Buckets.Result,
                            fileId,
                            contentType,
                            shallowStorageObject.Metadata);
                    }

                    // TODO: Notify user document has been completed and is ready for downloading
                    _backgroundJobs.Enqueue <NotifyConversionSuccessfulJob>(job => job.Run(fileId));
                }
                else if (result.TimedOut)
                {
                    // TODO: Let user know when task fails
                }
                else
                {
                    // TODO: Let user know when task fails
                }
            }
        }
Example #2
0
        private async Task <bool> ProcessJob(ConvertDocumentJob job)
        {
            try
            {
                _logger.LogInformation("Recieved '{JobName}' job.", job.Name);

                if (string.IsNullOrWhiteSpace(job.OriginalExtension))
                {
                    _logger.LogError("Job '{JobName}' failed because it didn't contain the original extension.", job.Name);
                }

                var fileName = job.FileId + "." + job.OriginalExtension;
                var fullPath = Path.Combine(_workFolder, fileName);

                using (var file = File.OpenWrite(fullPath))
                {
                    var storageFile = await _provider.GetObject(Shared.Constants.Buckets.Original, job.FileId);

                    using (var stream = storageFile.Data)
                    {
                        await stream.CopyToAsync(file);
                    }
                }

                var result = Converter.Convert(fullPath, job.DesiredExtension, _workFolder);
                if (result.Successful)
                {
                    var resultPath = await CopyFile(result.OutputFile, _outputFolder);

                    var info = new FileInfo(resultPath);

                    var contentType = KitchenSink.GetContentTypeFromExtension(info.Extension);

                    using (var file = File.OpenRead(resultPath))
                    {
                        await _provider.UploadObject(file, Shared.Constants.Buckets.Result, job.FileId, contentType);
                    }

                    var notifyJob = new NotifyUserJob
                    {
                        IsSuccessful = true,
                        FileId       = job.FileId
                    };

                    _messageQueue.QueueJob(notifyJob);

                    _logger.LogInformation("Job '{JobName}' was processed successfuly. Took {milliseconds} ms.", job.Name, result.Time.TotalMilliseconds);
                    return(true);
                }
                else if (result.TimedOut)
                {
                    _logger.LogError("Job '{JobName}' timed out after {milliseconds} ms.", job.Name, result.Time.TotalMilliseconds);

                    // TODO: Let user know when task fails
                }
                else
                {
                    _logger.LogError("Job '{JobName}' failed. Reason: {Reason} Took {milliseconds} ms.", job.Name, result.Output, result.Time.TotalMilliseconds);

                    // TODO: Let user know when task fails
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Job 'JobName' failed because: {Message}.", job.Name, ex.Message);
            }

            return(false);
        }