Feature: Device Batch Attachments

allows for attachments to be uploaded and associated with Device Batches
This commit is contained in:
Gary Sharp
2020-11-29 16:41:20 +11:00
parent e531ffe2b7
commit 28e5901929
26 changed files with 2153 additions and 320 deletions
@@ -38,6 +38,21 @@ namespace Disco.Services
DocumentTemplateManagedGroups.TriggerDeviceAttachmentDeleted(Database, attachmentId, documentTemplateId, deviceSerialNumber);
}
public static bool CanDelete(this DeviceBatchAttachment attachment)
{
if (UserService.CurrentAuthorization.Has(Claims.Config.DeviceBatch.Configure))
return true;
return false;
}
public static void OnDelete(this DeviceBatchAttachment attachment, DiscoDataContext Database)
{
if (!attachment.CanDelete())
throw new InvalidOperationException("Deletion of Attachment is Denied");
attachment.RepositoryDelete(Database);
Database.DeviceBatchAttachments.Remove(attachment);
}
public static bool CanDelete(this JobAttachment ja)
{
if (UserService.CurrentAuthorization.Has(Claims.Job.Actions.RemoveAnyAttachments))
@@ -9,39 +9,45 @@ namespace Disco.Services
public static class AttachmentDataStoreExtensions
{
public static string RepositoryFilename(this IAttachment Attachment, DiscoDataContext Database)
public static string RepositoryFilename(this IAttachment attachment, DiscoDataContext database)
{
switch (Attachment.AttachmentType)
switch (attachment.AttachmentType)
{
case AttachmentTypes.Device:
return Path.Combine(DataStore.CreateLocation(Database, "DeviceAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_file", Attachment.Reference, Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "DeviceAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_file");
case AttachmentTypes.DeviceBatch:
return Path.Combine(DataStore.CreateLocation(database, "DeviceBatchAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_file");
case AttachmentTypes.Job:
return Path.Combine(DataStore.CreateLocation(Database, "JobAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_file", Attachment.Reference, Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "JobAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_file");
case AttachmentTypes.User:
return Path.Combine(DataStore.CreateLocation(Database, "UserAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_file", ((string)Attachment.Reference).Replace('\\', '_'), Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "UserAttachments", attachment.Timestamp),
$"{((string)attachment.Reference).Replace('\\', '_')}_{attachment.Id}_file");
default:
throw new ArgumentException("Unknown Attachment Type", nameof(Attachment));
throw new ArgumentException("Unknown Attachment Type", nameof(attachment));
}
}
public static string RepositoryThumbnailFilename(this IAttachment Attachment, DiscoDataContext Database)
public static string RepositoryThumbnailFilename(this IAttachment attachment, DiscoDataContext database)
{
switch (Attachment.AttachmentType)
switch (attachment.AttachmentType)
{
case AttachmentTypes.Device:
return Path.Combine(DataStore.CreateLocation(Database, "DeviceAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_thumb.jpg", Attachment.Reference, Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "DeviceAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_thumb.jpg");
case AttachmentTypes.DeviceBatch:
return Path.Combine(DataStore.CreateLocation(database, "DeviceBatchAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_thumb.jpg");
case AttachmentTypes.Job:
return Path.Combine(DataStore.CreateLocation(Database, "JobAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_thumb.jpg", Attachment.Reference, Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "JobAttachments", attachment.Timestamp),
$"{attachment.Reference}_{attachment.Id}_thumb.jpg");
case AttachmentTypes.User:
return Path.Combine(DataStore.CreateLocation(Database, "UserAttachments", Attachment.Timestamp),
string.Format("{0}_{1}_thumb.jpg", ((string)Attachment.Reference).Replace('\\', '_'), Attachment.Id));
return Path.Combine(DataStore.CreateLocation(database, "UserAttachments", attachment.Timestamp),
$"{((string)attachment.Reference).Replace('\\', '_')}_{attachment.Id}_thumb.jpg");
default:
throw new ArgumentException("Unknown Attachment Type", nameof(Attachment));
throw new ArgumentException("Unknown Attachment Type", nameof(attachment));
}
}
@@ -0,0 +1,72 @@
using Disco.Data.Repository;
using Disco.Data.Repository.Monitor;
using Disco.Models.Repository;
using Disco.Services.Authorization;
using Disco.Services.Web.Signalling;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace Disco.Services.Devices
{
[HubName("deviceBatchUpdates"), DiscoHubAuthorizeAny(Claims.Config.DeviceBatch.Show, Claims.Config.DeviceBatch.Configure)]
public class DeviceBatchUpdatesHub : Hub
{
private const string UserPrefix = "DeviceBatch_";
public static IHubContext HubContext { get; private set; }
private static IDisposable RepositoryBeforeSubscription;
private static IDisposable RepositoryAfterSubscription;
static DeviceBatchUpdatesHub()
{
HubContext = GlobalHost.ConnectionManager.GetHubContext<DeviceBatchUpdatesHub>();
// Subscribe to Repository Monitor for Changes
RepositoryBeforeSubscription = RepositoryMonitor.StreamBeforeCommit
.Where(e => e.EntityType == typeof(DeviceBatchAttachment) && e.EventType == RepositoryMonitorEventType.Deleted)
.Subscribe(RepositoryEventBefore);
RepositoryAfterSubscription = RepositoryMonitor.StreamAfterCommit
.Where(e => e.EntityType == typeof(DeviceBatchAttachment) && e.EventType == RepositoryMonitorEventType.Added)
.Subscribe(RepositoryAfterEvent);
}
private static string GroupName(int deviceBatchId)
{
return $"{UserPrefix}{deviceBatchId}";
}
public override Task OnConnected()
{
if (!int.TryParse(Context.QueryString["DeviceBatchId"], out var deviceBatchId))
throw new ArgumentNullException("DeviceBatchId");
Groups.Add(Context.ConnectionId, GroupName(deviceBatchId));
return base.OnConnected();
}
private static void RepositoryEventBefore(RepositoryMonitorEvent e)
{
if (e.EventType == RepositoryMonitorEventType.Deleted && e.Entity is DeviceBatchAttachment attachment)
{
using (DiscoDataContext Database = new DiscoDataContext())
{
var deviceBatchId = Database.DeviceBatchAttachments.Where(a => a.Id == attachment.Id).Select(a => a.DeviceBatchId).First();
HubContext.Clients.Group(GroupName(deviceBatchId)).removeAttachment(attachment.Id);
}
}
}
private static void RepositoryAfterEvent(RepositoryMonitorEvent e)
{
if (e.EventType == RepositoryMonitorEventType.Added && e.Entity is DeviceBatchAttachment attachment)
{
HubContext.Clients.Group(GroupName(attachment.DeviceBatchId)).addAttachment(attachment.Id);
}
}
}
}
+8 -1
View File
@@ -243,6 +243,7 @@
<Compile Include="Devices\DeviceExtensions.cs" />
<Compile Include="Devices\DeviceModelExtensions.cs" />
<Compile Include="Devices\DeviceProfileExtensions.cs" />
<Compile Include="Devices\DeviceBatchUpdatesHub.cs" />
<Compile Include="Devices\DeviceWirelessProfileExtensions.cs" />
<Compile Include="Devices\Enrolment\DeviceEnrolment.cs" />
<Compile Include="Devices\Enrolment\EnrolmentLog.cs" />
@@ -325,6 +326,7 @@
<Compile Include="Extensions\DateTimeExtensions.cs" />
<Compile Include="Extensions\EnumerableExtensions.cs" />
<Compile Include="Extensions\ImagingExtensions.cs" />
<Compile Include="Extensions\MeasurementUnitExtensions.cs" />
<Compile Include="Extensions\RxExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\UIHelpers.cs" />
@@ -335,7 +337,7 @@
<Compile Include="Interop\ActiveDirectory\ActiveDirectoryManagedGroups.cs" />
<Compile Include="Interop\ActiveDirectory\ADDeviceDescriptionUpdateTask.cs" />
<Compile Include="Interop\ActiveDirectory\ADDirectoryEntry.cs" />
<Compile Include="Interop\ActiveDirectory\ADDiscoverForestServers.cs" />
<Compile Include="Interop\ActiveDirectory\ADDiscoverServers.cs" />
<Compile Include="Interop\ActiveDirectory\ADDomain.cs" />
<Compile Include="Interop\ActiveDirectory\ADDomainController.cs" />
<Compile Include="Interop\ActiveDirectory\ADGroup.cs" />
@@ -532,6 +534,11 @@
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_StartDate="2000/1/1" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">