1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

return attachments from API

This commit is contained in:
Kyle Spearrin
2017-06-30 23:01:41 -04:00
parent 284078e946
commit 8ea81a74ae
10 changed files with 132 additions and 31 deletions

View File

@ -0,0 +1,46 @@
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Bit.Core.Models.Api
{
public class AttachmentResponseModel : ResponseModel
{
public AttachmentResponseModel(string id, CipherAttachment.MetaData data, Cipher cipher, GlobalSettings globalSettings)
: base("attachment")
{
Id = id;
Url = $"{globalSettings.Attachment.BaseUrl}{cipher.Id}/{id}";
FileName = data.FileName;
Size = data.SizeString;
SizeName = Utilities.CoreHelpers.ReadableBytesSize(data.Size);
}
public string Id { get; set; }
public string Url { get; set; }
public string FileName { get; set; }
public string Size { get; set; }
public string SizeName { get; set; }
public static IEnumerable<AttachmentResponseModel> FromCipher(Cipher cipher, GlobalSettings globalSettings)
{
if(string.IsNullOrWhiteSpace(cipher.Attachments))
{
return null;
}
try
{
var attachments =
JsonConvert.DeserializeObject<Dictionary<string, CipherAttachment.MetaData>>(cipher.Attachments);
return attachments.Select(a => new AttachmentResponseModel(a.Key, a.Value, cipher, globalSettings));
}
catch
{
return null;
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace Bit.Core.Models.Api
{
public class CipherMiniResponseModel : ResponseModel
{
public CipherMiniResponseModel(Cipher cipher, string obj = "cipherMini")
public CipherMiniResponseModel(Cipher cipher, GlobalSettings globalSettings, string obj = "cipherMini")
: base(obj)
{
if(cipher == null)
@ -20,6 +20,7 @@ namespace Bit.Core.Models.Api
Type = cipher.Type;
RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
switch(cipher.Type)
{
@ -35,13 +36,14 @@ namespace Bit.Core.Models.Api
public string OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public dynamic Data { get; set; }
public IEnumerable<AttachmentResponseModel> Attachments { get; set; }
public DateTime RevisionDate { get; set; }
}
public class CipherResponseModel : CipherMiniResponseModel
{
public CipherResponseModel(CipherDetails cipher, string obj = "cipher")
: base(cipher, obj)
public CipherResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "cipher")
: base(cipher, globalSettings, obj)
{
FolderId = cipher.FolderId?.ToString();
Favorite = cipher.Favorite;
@ -55,9 +57,9 @@ namespace Bit.Core.Models.Api
public class CipherDetailsResponseModel : CipherResponseModel
{
public CipherDetailsResponseModel(CipherDetails cipher,
public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherDetails")
: base(cipher, obj)
: base(cipher, globalSettings, obj)
{
if(collectionCiphers.ContainsKey(cipher.Id))
{
@ -69,9 +71,9 @@ namespace Bit.Core.Models.Api
}
}
public CipherDetailsResponseModel(CipherDetails cipher, IEnumerable<CollectionCipher> collectionCiphers,
string obj = "cipherDetails")
: base(cipher, obj)
public CipherDetailsResponseModel(CipherDetails cipher, GlobalSettings globalSettings,
IEnumerable<CollectionCipher> collectionCiphers, string obj = "cipherDetails")
: base(cipher, globalSettings, obj)
{
CollectionIds = collectionCiphers.Select(c => c.CollectionId);
}
@ -81,9 +83,9 @@ namespace Bit.Core.Models.Api
public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
{
public CipherMiniDetailsResponseModel(Cipher cipher,
public CipherMiniDetailsResponseModel(Cipher cipher, GlobalSettings globalSettings,
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherMiniDetails")
: base(cipher, obj)
: base(cipher, globalSettings, obj)
{
if(collectionCiphers.ContainsKey(cipher.Id))
{

View File

@ -1,12 +1,13 @@
using System;
using Core.Models.Data;
using Bit.Core.Models.Table;
using System.Collections.Generic;
namespace Bit.Core.Models.Api
{
public class LoginResponseModel : ResponseModel
{
public LoginResponseModel(Cipher cipher, string obj = "login")
public LoginResponseModel(Cipher cipher, GlobalSettings globalSettings, string obj = "login")
: base(obj)
{
if(cipher == null)
@ -30,10 +31,11 @@ namespace Bit.Core.Models.Api
Notes = data.Notes;
RevisionDate = cipher.RevisionDate;
Edit = true;
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
}
public LoginResponseModel(CipherDetails cipher, string obj = "login")
: this(cipher as Cipher, obj)
public LoginResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "login")
: this(cipher as Cipher, globalSettings, obj)
{
FolderId = cipher.FolderId?.ToString();
Favorite = cipher.Favorite;
@ -50,6 +52,7 @@ namespace Bit.Core.Models.Api
public string Username { get; set; }
public string Password { get; set; }
public string Notes { get; set; }
public IEnumerable<AttachmentResponseModel> Attachments { get; set; }
public DateTime RevisionDate { get; set; }
}
}