feature: document handlers

This commit is contained in:
Gary Sharp
2022-12-03 14:32:05 +11:00
parent 96cccab958
commit 13e666d95a
26 changed files with 1027 additions and 120 deletions
@@ -105,6 +105,9 @@ namespace Disco.Services
#endregion
public static DeviceAttachment CreateAttachment(this Device Device, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null)
=> Device.CreateAttachment(Database, CreatorUser, Filename, DateTime.Now, MimeType, Comments, Content, DocumentTemplate, PdfThumbnail);
public static DeviceAttachment CreateAttachment(this Device Device, DiscoDataContext Database, User CreatorUser, string Filename, DateTime timestamp, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null, string HandlerId = null, string HandlerReferenceId = null, string HandlerData = null)
{
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.OrdinalIgnoreCase))
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
@@ -115,8 +118,11 @@ namespace Disco.Services
TechUserId = CreatorUser.UserId,
Filename = Filename,
MimeType = MimeType,
Timestamp = DateTime.Now,
Comments = Comments
Timestamp = timestamp,
Comments = Comments,
HandlerId = HandlerId,
HandlerReferenceId = HandlerReferenceId,
HandlerData = HandlerData,
};
if (DocumentTemplate != null)
@@ -136,6 +142,9 @@ namespace Disco.Services
}
public static JobAttachment CreateAttachment(this Job Job, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null)
=> Job.CreateAttachment(Database, CreatorUser, Filename, DateTime.Now, MimeType, Comments, Content, DocumentTemplate, PdfThumbnail);
public static JobAttachment CreateAttachment(this Job Job, DiscoDataContext Database, User CreatorUser, string Filename, DateTime Timestamp, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null, string HandlerId = null, string HandlerReferenceId = null, string HandlerData = null)
{
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.OrdinalIgnoreCase))
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
@@ -146,8 +155,11 @@ namespace Disco.Services
TechUserId = CreatorUser.UserId,
Filename = Filename,
MimeType = MimeType,
Timestamp = DateTime.Now,
Comments = Comments
Timestamp = Timestamp,
Comments = Comments,
HandlerId = HandlerId,
HandlerReferenceId = HandlerReferenceId,
HandlerData = HandlerData,
};
if (DocumentTemplate != null)
@@ -167,6 +179,9 @@ namespace Disco.Services
}
public static UserAttachment CreateAttachment(this User User, DiscoDataContext Database, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null)
=> User.CreateAttachment(Database, CreatorUser, Filename, DateTime.Now, MimeType, Comments, Content, DocumentTemplate, PdfThumbnail);
public static UserAttachment CreateAttachment(this User User, DiscoDataContext Database, User CreatorUser, string Filename, DateTime Timestamp, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, Image PdfThumbnail = null, string HandlerId = null, string HandlerReferenceId = null, string HandlerData = null)
{
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.OrdinalIgnoreCase))
MimeType = Interop.MimeTypes.ResolveMimeType(Filename);
@@ -177,8 +192,11 @@ namespace Disco.Services
TechUserId = CreatorUser.UserId,
Filename = Filename,
MimeType = MimeType,
Timestamp = DateTime.Now,
Comments = Comments
Timestamp = Timestamp,
Comments = Comments,
HandlerId = HandlerId,
HandlerReferenceId = HandlerReferenceId,
HandlerData = HandlerData,
};
if (DocumentTemplate != null)
@@ -165,12 +165,12 @@ namespace Disco.Services.Documents.AttachmentImport
}
}
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, string PdfFilename)
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, string PdfFilename)
{
return ImportPdfAttachment(Identifier, Database, PdfFilename, null);
}
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, string PdfFilename, Image Thumbnail)
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, string PdfFilename, Image Thumbnail)
{
using (var pdfStream = File.OpenRead(PdfFilename))
{
@@ -178,17 +178,20 @@ namespace Disco.Services.Documents.AttachmentImport
}
}
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent)
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent)
{
return ImportPdfAttachment(Identifier, Database, PdfContent, null, new List<DocumentUniqueIdentifier>() { Identifier });
}
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent, Image Thumbnail)
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent, Image Thumbnail)
{
return ImportPdfAttachment(Identifier, Database, PdfContent, Thumbnail, new List<DocumentUniqueIdentifier>() { Identifier });
}
public static bool ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent, Image Thumbnail, List<DocumentUniqueIdentifier> PageIdentifiers)
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent, Image Thumbnail, List<DocumentUniqueIdentifier> PageIdentifiers)
=> Identifier.ImportPdfAttachment(Database, PdfContent, Thumbnail, PageIdentifiers, null, null, null, null);
public static IAttachment ImportPdfAttachment(this DocumentUniqueIdentifier Identifier, DiscoDataContext Database, Stream PdfContent, Image Thumbnail, List<DocumentUniqueIdentifier> PageIdentifiers, DateTime? Timestamp, string HandlerId, string HandlerReferenceId, string HandlerData)
{
string filename;
string comments;
@@ -196,16 +199,32 @@ namespace Disco.Services.Documents.AttachmentImport
if (Identifier.DocumentTemplate == null)
{
filename = $"{Identifier.Target.AttachmentReferenceId.Replace('\\', '_')}_{Identifier.TimeStamp:yyyyMMdd-HHmmss}.pdf";
comments = $"Uploaded: {Identifier.TimeStamp:s}";
if (Timestamp.HasValue)
{
filename = $"{Identifier.Target.AttachmentReferenceId.Replace('\\', '_')}_{Timestamp:yyyyMMdd-HHmmss}.pdf";
comments = $"Completed: {Timestamp:s}";
}
else
{
filename = $"{Identifier.Target.AttachmentReferenceId.Replace('\\', '_')}_{Identifier.TimeStamp:yyyyMMdd-HHmmss}.pdf";
comments = $"Uploaded: {Identifier.TimeStamp:s}";
}
}
else
{
filename = $"{Identifier.DocumentTemplateId}_{Identifier.TimeStamp:yyyyMMdd-HHmmss}.pdf";
comments = string.Format("Generated: {0:s}", Identifier.TimeStamp);
if (Timestamp.HasValue)
{
filename = $"{Identifier.DocumentTemplateId}_{Timestamp:yyyyMMdd-HHmmss}.pdf";
comments = $"Generated: {Identifier.TimeStamp:s}; Completed: {Timestamp:s}";
}
else
{
filename = $"{Identifier.DocumentTemplateId}_{Identifier.TimeStamp:yyyyMMdd-HHmmss}.pdf";
comments = $"Generated: {Identifier.TimeStamp:s}";
}
}
User creatorUser = UserService.GetUser(Identifier.CreatorId, Database, true);
User creatorUser = UserService.GetUser(Identifier.CreatorId, Database, false);
if (creatorUser == null)
{
// No Creator User (or Username invalid)
@@ -216,18 +235,18 @@ namespace Disco.Services.Documents.AttachmentImport
{
case AttachmentTypes.Device:
Device d = (Device)Identifier.Target;
attachment = d.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail);
attachment = d.CreateAttachment(Database, creatorUser, filename, Timestamp ?? DateTime.Now, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail, HandlerId, HandlerReferenceId, HandlerData);
break;
case AttachmentTypes.Job:
Job j = (Job)Identifier.Target;
attachment = j.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail);
attachment = j.CreateAttachment(Database, creatorUser, filename, Timestamp ?? DateTime.Now, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail, HandlerId, HandlerReferenceId, HandlerData);
break;
case AttachmentTypes.User:
User u = (User)Identifier.Target;
attachment = u.CreateAttachment(Database, creatorUser, filename, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail);
attachment = u.CreateAttachment(Database, creatorUser, filename, Timestamp ?? DateTime.Now, DocumentTemplate.PdfMimeType, comments, PdfContent, Identifier.DocumentTemplate, Thumbnail, HandlerId, HandlerReferenceId, HandlerData);
break;
default:
return false;
return null;
}
if (Identifier.DocumentTemplate != null && !string.IsNullOrWhiteSpace(Identifier.DocumentTemplate.OnImportAttachmentExpression))
@@ -242,7 +261,8 @@ namespace Disco.Services.Documents.AttachmentImport
SystemLog.LogException("Document Importer - OnImportAttachmentExpression", ex);
}
}
return true;
return attachment;
}
}
}
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Disco
{
@@ -26,4 +28,61 @@ namespace Disco
}
}
public static class OneOf
{
public static OneOf<T> Create<T>(T instance)
=> OneOf<T>.Create(instance);
}
public struct OneOf<T> : IEnumerable<T>
{
private readonly T instance;
private OneOf(T instance)
{
this.instance = instance;
}
public static OneOf<T> Create(T instance)
=> new OneOf<T>(instance);
public IEnumerator<T> GetEnumerator()
=> new OneOfEnumerator(instance);
IEnumerator IEnumerable.GetEnumerator()
=> new OneOfEnumerator(instance);
private struct OneOfEnumerator : IEnumerator<T>
{
private readonly T instance;
private bool moved;
public OneOfEnumerator(T instance)
{
this.instance = instance;
moved = false;
}
public T Current => instance;
object IEnumerator.Current => instance;
public void Dispose() { }
public bool MoveNext()
{
if (!moved)
{
moved = true;
return true;
}
return false;
}
public void Reset()
{
moved = false;
}
}
}
}
@@ -0,0 +1,32 @@
using Disco.Models.Repository;
using Disco.Models.Services.Documents;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace Disco.Services.Plugins.Features.DocumentHandlerProvider
{
[PluginFeatureCategory(DisplayName = "Document Handler")]
public abstract class DocumentHandlerProviderFeature : PluginFeature
{
public abstract string HandlerTitle { get; }
public abstract string HandlerDescription { get; }
public abstract bool CanHandle(DocumentTemplate template);
public abstract bool CanHandle(DocumentTemplatePackage templatePackage);
public abstract bool CanHandle(DocumentTemplate template, IAttachmentTarget target);
public abstract bool CanHandle(DocumentTemplatePackage templatePackage, IAttachmentTarget target);
public abstract bool CanHandleBulk(DocumentTemplate template);
public abstract bool CanHandleBulk(DocumentTemplatePackage templatePackage);
public abstract Type GenerationOptionsUi { get; }
public virtual string GenerationOptionsIcon => "file-text-o";
public abstract object GetGenerationOptionsUiModel(DocumentTemplate template, IAttachmentTarget target, User targetUser, User techUser);
public abstract object GetGenerationOptionsUiModel(DocumentTemplatePackage templatePackage, IAttachmentTarget target, User targetUser, User techUser);
public abstract ActionResult Handle(DocumentTemplate template, IAttachmentTarget target, User targetUser, User techUser);
public abstract ActionResult Handle(DocumentTemplatePackage templatePackage, IAttachmentTarget target, User targetUser, User techUser);
public abstract ActionResult HandleBulk(DocumentTemplate template, IList<IAttachmentTarget> targets, User targetUser, User techUser);
public abstract ActionResult HandleBulk(DocumentTemplatePackage templatePackage, IList<IAttachmentTarget> targets, User targetUser, User techUser);
}
}