|
@@ -1,46 +1,115 @@
|
|
|
using InABox.Core;
|
|
|
+using NPOI.POIFS.FileSystem;
|
|
|
|
|
|
namespace InABox.Database;
|
|
|
|
|
|
public class DocumentStore : Store<Document>
|
|
|
{
|
|
|
- protected override void AfterLoad(IEnumerable<Document> items)
|
|
|
+ private static readonly Column<Document> DataColumn = new(x => x.Data);
|
|
|
+
|
|
|
+ protected override CoreTable OnQuery(Filter<Document>? filter, Columns<Document>? columns, SortOrder<Document>? sort)
|
|
|
{
|
|
|
- base.AfterLoad(items);
|
|
|
- //if (!Provider.IsRelational())
|
|
|
- //{
|
|
|
- // foreach (var item in items)
|
|
|
- // {
|
|
|
- // if ((item.Data == null) || (item.Data.Length == 0))
|
|
|
- // item.Data = Provider.LoadFile(item.ID);
|
|
|
- // }
|
|
|
- //}
|
|
|
+ var getData = false;
|
|
|
+ if (columns is null || columns.Contains(DataColumn))
|
|
|
+ {
|
|
|
+ getData = true;
|
|
|
+ columns?.Add(x => x.ID);
|
|
|
+ }
|
|
|
+
|
|
|
+ var result = base.OnQuery(filter, columns, sort);
|
|
|
+
|
|
|
+ if (getData)
|
|
|
+ {
|
|
|
+ foreach(var row in result.Rows)
|
|
|
+ {
|
|
|
+ var id = row.Get<Document, Guid>(x => x.ID);
|
|
|
+ var filename = FileName(id);
|
|
|
+ if (File.Exists(filename))
|
|
|
+ {
|
|
|
+ var data = File.ReadAllBytes(filename);
|
|
|
+ row.Set<Document, byte[]>(x => x.Data, data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
protected override void OnSave(Document entity, ref string auditnote)
|
|
|
{
|
|
|
- //if (!Provider.IsRelational())
|
|
|
- //{
|
|
|
- // byte[] data = entity.Data;
|
|
|
- // entity.Data = new byte[] { };
|
|
|
- // base.OnSave(entity);
|
|
|
- // entity.Data = data;
|
|
|
- // Provider.SaveFile(entity.ID, data);
|
|
|
- //}
|
|
|
- //else
|
|
|
+ byte[]? data = null;
|
|
|
+ if(entity.Data != null && entity.Data.Length > 0)
|
|
|
+ {
|
|
|
+ data = entity.Data;
|
|
|
+ entity.Data = Array.Empty<byte>();
|
|
|
+ }
|
|
|
base.OnSave(entity, ref auditnote);
|
|
|
+
|
|
|
+ if(data is not null)
|
|
|
+ {
|
|
|
+ // Set data back so that it's not obvious what we've done.
|
|
|
+ entity.Data = data;
|
|
|
+ EnsureDirectory();
|
|
|
+ SaveData(entity.ID, data);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
protected override void OnSave(IEnumerable<Document> entities, ref string auditnote)
|
|
|
{
|
|
|
- foreach (var entity in entities)
|
|
|
- OnSave(entity, ref auditnote);
|
|
|
+ var entityList = entities.AsIList();
|
|
|
+ var dataList = entityList.Select(x =>
|
|
|
+ {
|
|
|
+ byte[]? data = null;
|
|
|
+ if(x.Data != null && x.Data.Length > 0)
|
|
|
+ {
|
|
|
+ data = x.Data;
|
|
|
+ x.Data = Array.Empty<byte>();
|
|
|
+ }
|
|
|
+ return (data, x);
|
|
|
+ }).ToList();
|
|
|
+
|
|
|
+ base.OnSave(entityList, ref auditnote);
|
|
|
+
|
|
|
+ EnsureDirectory();
|
|
|
+ foreach (var item in dataList)
|
|
|
+ {
|
|
|
+ if(item.data is not null)
|
|
|
+ {
|
|
|
+ // Set data back so that it's not obvious what we've done.
|
|
|
+ item.x.Data = item.data;
|
|
|
+ SaveData(item.x.ID, item.data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnDelete(Document entity)
|
|
|
+ {
|
|
|
+ base.OnDelete(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnDelete(IEnumerable<Document> entities)
|
|
|
+ {
|
|
|
+ base.OnDelete(entities);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SaveData(Guid documentID, byte[] data)
|
|
|
+ {
|
|
|
+ File.WriteAllBytes(FileName(documentID), data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private string Directory()
|
|
|
+ {
|
|
|
+ var directory = Path.GetDirectoryName(Provider.URL);
|
|
|
+ var filename = Path.GetFileName(Provider.URL);
|
|
|
+ return Path.Combine(directory ?? "", $"{filename}.data", nameof(Document));
|
|
|
+ }
|
|
|
+
|
|
|
+ private string FileName(Guid documentID)
|
|
|
+ {
|
|
|
+ return Path.Combine(Directory(), documentID.ToString());
|
|
|
}
|
|
|
|
|
|
- protected override void AfterDelete(Document entity)
|
|
|
+ private void EnsureDirectory()
|
|
|
{
|
|
|
- base.AfterDelete(entity);
|
|
|
- //if (!Provider.IsRelational())
|
|
|
- // Provider.DeleteFile(entity.ID);
|
|
|
+ System.IO.Directory.CreateDirectory(Directory());
|
|
|
}
|
|
|
}
|