#180: bulk download device/job/user attachments
This commit is contained in:
@@ -9,6 +9,8 @@ using Disco.Services.Users;
|
||||
using Disco.Services.Web;
|
||||
using System;
|
||||
using System.Data.Entity;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Mvc;
|
||||
@@ -124,6 +126,46 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
return HttpNotFound("Invalid Attachment Number");
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.User.ShowAttachments)]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public virtual ActionResult AttachmentDownloadAll(string id)
|
||||
{
|
||||
id = ActiveDirectory.ParseDomainAccountId(id);
|
||||
|
||||
var user = Database.Users
|
||||
.Include(u => u.UserAttachments)
|
||||
.Where(u => u.UserId == id)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (user == null || user.UserAttachments.Count == 0)
|
||||
return NotFound();
|
||||
|
||||
var responseStream = new MemoryStream();
|
||||
using (var archive = new ZipArchive(responseStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var attachment in user.UserAttachments)
|
||||
{
|
||||
var repoFileName = attachment.RepositoryFilename(Database);
|
||||
if (System.IO.File.Exists(repoFileName))
|
||||
{
|
||||
var fileName = $"{Path.GetFileNameWithoutExtension(attachment.Filename)}-{attachment.Timestamp:yyyyMMdd-HHmmss}{Path.GetExtension(attachment.Filename)}";
|
||||
|
||||
var entry = archive.CreateEntry(fileName, CompressionLevel.Fastest);
|
||||
entry.LastWriteTime = attachment.Timestamp;
|
||||
using (var entryStream = entry.Open())
|
||||
{
|
||||
using (var attachmentStream = System.IO.File.OpenRead(repoFileName))
|
||||
{
|
||||
attachmentStream.CopyTo(entryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
responseStream.Position = 0;
|
||||
return File(responseStream, "application/zip", $"{user.UserId.Replace('\\', '_')}_UserAttachments_{DateTime.Now:yyyyMMdd-HHmmss}.zip");
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.User.ShowAttachments)]
|
||||
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Client, Duration = 172800)]
|
||||
public virtual ActionResult AttachmentThumbnail(int id)
|
||||
|
||||
Reference in New Issue
Block a user