internal void UploadFile(String jobFile, DocumentImportTask task) { String fname = ""; try { TenantContext.Enter(task.Tenant); if (!task.Uri.IsFile) { LogAndThrow("Error importing task file {0}: Uri is not a file: {1}", jobFile, task.Uri); } fname = task.Uri.LocalPath; if (FileHasImportFailureMarker(fname, task.FileTimestamp)) { return; } if (!File.Exists(fname)) { LogAndThrow("Error importing task file {0}: File missing: {1}", jobFile, fname); } var blobStore = GetBlobStoreForTenant(); var identityGenerator = GetIdentityGeneratorForTenant(); if (blobStore == null || identityGenerator == null) { Logger.ErrorFormat("Tenant {1} not found or not configured for file: {1}", task.Tenant, fname); return; } BlobId blobId; if (!String.IsNullOrEmpty(task.FileName)) { //use the real file name from the task not the name of the file using (FileStream fs = File.Open(fname, FileMode.Open, FileAccess.Read)) { blobId = blobStore.Upload(task.Format, new FileNameWithExtension(task.FileName), fs); } } else { //No filename given in task, use name of the blob blobId = blobStore.Upload(task.Format, fname); } if (task.Format == OriginalFormat) { var descriptor = blobStore.GetDescriptor(blobId); var fileName = new FileNameWithExtension(task.FileName); var handleInfo = new DocumentHandleInfo(task.Handle, fileName, task.CustomData); var documentId = identityGenerator.New <DocumentDescriptorId>(); var createDocument = new InitializeDocumentDescriptor( documentId, blobId, handleInfo, descriptor.Hash, fileName ); _commandBus.Send(createDocument, "import-from-file"); } else { var reader = _tenantAccessor.Current.Container.Resolve <IDocumentWriter>(); var handle = reader.FindOneById(task.Handle); var documentId = handle.DocumentDescriptorId; var command = new AddFormatToDocumentDescriptor( documentId, task.Format, blobId, new PipelineId("user-content") ); _commandBus.Send(command, "import-from-file"); } TaskExecuted(task); DeleteImportFailure(fname); } catch (Exception ex) { Logger.ErrorFormat(ex, "Job Import Queue - Error importing {0} - {1}", jobFile, ex.Message); ImportFailure failure = new ImportFailure() { Error = ex.ToString(), FileName = fname, Timestamp = DateTime.Now, ImportFileTimestampTicks = task.FileTimestamp.Ticks, }; MarkImportFailure(failure); } finally { TenantContext.Exit(); } }
public async Task <HttpResponseMessage> AddFormatToDocument(TenantId tenantId, DocumentFormat format) { var errorMessage = await AddFormatFromHttpContent(Request.Content, format); Logger.DebugFormat("File {0} processed with message {1}", _blobId, errorMessage); if (errorMessage != null) { Logger.Error("Error Adding format To Document: " + errorMessage); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, errorMessage )); } String queueName = _customData[AddFormatToDocumentParameters.QueueName] as String; String jobId = _customData[AddFormatToDocumentParameters.JobId] as String; DocumentDescriptorId documentId; if (String.IsNullOrEmpty(queueName)) { //user ask for handle, we need to grab the handle var documentHandle = new DocumentHandle(_customData[AddFormatToDocumentParameters.DocumentHandle] as String); var handle = _handleWriter.FindOneById(documentHandle); documentId = handle.DocumentDescriptorId; if (documentId == null) { Logger.ErrorFormat("Trying to add a format for Handle {0} with a null DocumentId", documentHandle); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, "" )); } Logger.DebugFormat("Add format {0} to handle {1} and document id {2}", format, handle, documentId); } else { var job = _queueDispatcher.GetJob(queueName, jobId); if (job == null) { Logger.WarnFormat("Job id {0} not found in queue {1}", jobId, queueName); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, String.Format("Job id {0} not found in queue {1}", jobId, queueName))); } documentId = job.DocumentDescriptorId; if (documentId == null) { Logger.ErrorFormat("Trying to add a format for Job Id {0} queue {1} - Job has DocumentDescriptorId null", jobId, queueName); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, "" )); } //need to check if the descriptor is deleted var exists = _documentDescriptorReader .AllUnsorted .Where(d => d.Id == documentId) .Any(); if (!exists) { Logger.ErrorFormat("Trying to add a format for Job Id {0} queue {1} - DocumentDescriptor does not exists or was deleted!", jobId, queueName); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, "" )); } Logger.DebugFormat("Add format {0} to job id {1} and document id {2}", format, job.Id, documentId); } if (format == "null") { var formatFromFileName = _documentFormatTranslator.GetFormatFromFileName(_fileName); if (formatFromFileName == null) { String error = "Format not specified and no known format for file: " + _fileName; Logger.Error(error); return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, error )); } format = new DocumentFormat(formatFromFileName); } var createdById = new PipelineId(_customData[AddFormatToDocumentParameters.CreatedBy] as String); Logger.DebugFormat("Incoming new format for documentId {0}", documentId); var command = new AddFormatToDocumentDescriptor(documentId, format, _blobId, createdById); CommandBus.Send(command, "api"); return(Request.CreateResponse( HttpStatusCode.OK, new AddFormatToDocumentResponse { Result = true, } )); }