1 |
24e1c89d
|
Vojtěch Bartička
|
using Core.Contexts;
|
2 |
|
|
using Core.Entities;
|
3 |
|
|
using Models.Documents;
|
4 |
|
|
using Serilog;
|
5 |
|
|
using System;
|
6 |
|
|
using System.Collections.Generic;
|
7 |
|
|
using System.IO.Compression;
|
8 |
|
|
using System.Linq;
|
9 |
|
|
using System.Text;
|
10 |
|
|
using System.Threading.Tasks;
|
11 |
|
|
using System.Web;
|
12 |
5a08541d
|
Vojtěch Bartička
|
using AutoMapper;
|
13 |
|
|
using Models.Users;
|
14 |
3c185841
|
Vojtěch Bartička
|
using Ganss.XSS;
|
15 |
42bb0025
|
Vojtěch Bartička
|
using Microsoft.EntityFrameworkCore;
|
16 |
24e1c89d
|
Vojtěch Bartička
|
|
17 |
|
|
namespace Core.Services.DocumentService
|
18 |
|
|
{
|
19 |
|
|
public class DocumentServiceEF : IDocumentService
|
20 |
|
|
{
|
21 |
|
|
private readonly DatabaseContext databaseContext;
|
22 |
|
|
private readonly ILogger logger;
|
23 |
5a08541d
|
Vojtěch Bartička
|
private readonly IMapper mapper;
|
24 |
24e1c89d
|
Vojtěch Bartička
|
|
25 |
5a08541d
|
Vojtěch Bartička
|
public DocumentServiceEF(DatabaseContext databaseContext, ILogger logger, IMapper mapper)
|
26 |
24e1c89d
|
Vojtěch Bartička
|
{
|
27 |
|
|
this.databaseContext = databaseContext;
|
28 |
|
|
this.logger = logger;
|
29 |
5a08541d
|
Vojtěch Bartička
|
this.mapper = mapper;
|
30 |
24e1c89d
|
Vojtěch Bartička
|
}
|
31 |
|
|
|
32 |
|
|
/// <summary>
|
33 |
|
|
/// Adds the documents in request to the database
|
34 |
|
|
/// </summary>
|
35 |
|
|
/// <param name="request">request</param>
|
36 |
|
|
/// <param name="userId">GUID of the user</param>
|
37 |
|
|
/// <exception cref="InvalidOperationException">No User with passed GUID</exception>
|
38 |
|
|
/// <exception cref="FormatException">Error decoding BASE64 content</exception>
|
39 |
|
|
/// <exception cref="InvalidDataException">Zip file has wrong format</exception>
|
40 |
|
|
/// <exception cref="IOException">Error reading entries from the zip file</exception>
|
41 |
|
|
public void AddDocuments(DocumentAddRequest request, Guid userId)
|
42 |
|
|
{
|
43 |
|
|
User user = databaseContext.Users.Single(u => u.Id == userId);
|
44 |
ceb95b98
|
Vojtěch Bartička
|
int requiredAnnotations = int.Parse(databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey).Value);
|
45 |
005202c6
|
Vojtěch Bartička
|
|
46 |
24e1c89d
|
Vojtěch Bartička
|
foreach (var documentInfo in request.Documents)
|
47 |
|
|
{
|
48 |
52fe46f9
|
Vojtěch Bartička
|
if (documentInfo.Format == Models.Enums.EAddDocumentFormat.TEXTFILE)
|
49 |
24e1c89d
|
Vojtěch Bartička
|
{
|
50 |
7b2e66d3
|
Vojtěch Bartička
|
// TODO hardcoded UTF-8 - maybe do something smarter
|
51 |
|
|
var documentContent = Encoding.UTF8.GetString(Convert.FromBase64String(documentInfo.Content));
|
52 |
ceb95b98
|
Vojtěch Bartička
|
SaveDocument(documentContent, user, documentInfo.Name, requiredAnnotations);
|
53 |
24e1c89d
|
Vojtěch Bartička
|
}
|
54 |
7b2e66d3
|
Vojtěch Bartička
|
else if (documentInfo.Format == Models.Enums.EAddDocumentFormat.ZIP)
|
55 |
24e1c89d
|
Vojtěch Bartička
|
{
|
56 |
|
|
var (names, contents) = UnzipDocuments(documentInfo.Content);
|
57 |
|
|
for (int i = 0; i < names.Count; i++)
|
58 |
|
|
{
|
59 |
ceb95b98
|
Vojtěch Bartička
|
SaveDocument(contents[i], user, names[i], requiredAnnotations);
|
60 |
24e1c89d
|
Vojtěch Bartička
|
}
|
61 |
|
|
}
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/// <summary>
|
66 |
|
|
///
|
67 |
|
|
/// </summary>
|
68 |
|
|
/// <param name="base64encoded"></param>
|
69 |
|
|
/// <returns></returns>
|
70 |
|
|
private (List<string> Names, List<string> Contents) UnzipDocuments(string base64encoded)
|
71 |
|
|
{
|
72 |
|
|
List<string> names = new();
|
73 |
|
|
List<string> contents = new();
|
74 |
|
|
|
75 |
|
|
byte[] decoded = Convert.FromBase64String(base64encoded);
|
76 |
|
|
|
77 |
|
|
using (var zipStream = new MemoryStream(decoded))
|
78 |
|
|
using (var zipArchive = new ZipArchive(zipStream))
|
79 |
|
|
{
|
80 |
|
|
foreach (var entry in zipArchive.Entries)
|
81 |
|
|
{
|
82 |
|
|
names.Add(entry.Name);
|
83 |
|
|
using (var streamReader = new StreamReader(entry.Open()))
|
84 |
|
|
{
|
85 |
|
|
string text = streamReader.ReadToEnd();
|
86 |
|
|
contents.Add(text);
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
return (names, contents);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
ceb95b98
|
Vojtěch Bartička
|
private void SaveDocument(string content, User userAdded, string documentName, int requiredAnnotations)
|
95 |
24e1c89d
|
Vojtěch Bartička
|
{
|
96 |
|
|
DocumentContent documentContent = new DocumentContent()
|
97 |
|
|
{
|
98 |
|
|
Content = content
|
99 |
|
|
};
|
100 |
|
|
|
101 |
|
|
Document document = new Document()
|
102 |
|
|
{
|
103 |
|
|
DateAdded = DateTime.Now,
|
104 |
|
|
Content = documentContent,
|
105 |
|
|
UserAdded = userAdded,
|
106 |
|
|
Name = documentName,
|
107 |
|
|
Length = documentContent.Content.Length,
|
108 |
ceb95b98
|
Vojtěch Bartička
|
RequiredAnnotations = requiredAnnotations
|
109 |
24e1c89d
|
Vojtěch Bartička
|
};
|
110 |
|
|
|
111 |
|
|
databaseContext.DocumentContents.Add(documentContent);
|
112 |
|
|
databaseContext.Documents.Add(document);
|
113 |
5adba4c4
|
Vojtěch Bartička
|
databaseContext.SaveChanges();
|
114 |
24e1c89d
|
Vojtěch Bartička
|
}
|
115 |
|
|
|
116 |
42c654f6
|
Vojtěch Bartička
|
public DocumentListResponse GetDocuments(int pageIndex, int pageSize)
|
117 |
24e1c89d
|
Vojtěch Bartička
|
{
|
118 |
42c654f6
|
Vojtěch Bartička
|
var firstIndex = pageIndex * pageSize;
|
119 |
24e1c89d
|
Vojtěch Bartička
|
var documents = databaseContext.Documents.Select(d => d).ToList();
|
120 |
|
|
var totalCount = documents.Count;
|
121 |
42c654f6
|
Vojtěch Bartička
|
var pageCount = totalCount / pageSize;
|
122 |
24e1c89d
|
Vojtěch Bartička
|
if (pageCount == 0 && totalCount > 0)
|
123 |
|
|
{
|
124 |
|
|
pageCount = 1;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
7bbe8f15
|
Vojtěch Bartička
|
if (firstIndex > documents.Count - 1)
|
128 |
|
|
{
|
129 |
|
|
throw new Exception("Page index or page size too large");
|
130 |
|
|
}
|
131 |
|
|
|
132 |
42c654f6
|
Vojtěch Bartička
|
if (firstIndex + pageSize > documents.Count - 1)
|
133 |
7bbe8f15
|
Vojtěch Bartička
|
{
|
134 |
42c654f6
|
Vojtěch Bartička
|
pageSize = documents.Count - firstIndex;
|
135 |
7bbe8f15
|
Vojtěch Bartička
|
}
|
136 |
|
|
|
137 |
24e1c89d
|
Vojtěch Bartička
|
List<DocumentListInfo> documentInfos = new List<DocumentListInfo>();
|
138 |
42c654f6
|
Vojtěch Bartička
|
foreach (var document in documents.GetRange(firstIndex, pageSize))
|
139 |
24e1c89d
|
Vojtěch Bartička
|
{
|
140 |
8c9ce202
|
Vojtěch Bartička
|
var annotatingUsers = databaseContext.Annotations
|
141 |
|
|
.Where(a => !(a is FinalAnnotation))
|
142 |
|
|
.Where(a => a.Document == document)
|
143 |
|
|
.Select(a => a.User).ToList();
|
144 |
|
|
|
145 |
3cee56d3
|
Vojtěch Bartička
|
List<DocumentUserInfo> annotatingUsersDto = new();
|
146 |
85b8834f
|
Vojtěch Bartička
|
|
147 |
3cee56d3
|
Vojtěch Bartička
|
// Include annotation state
|
148 |
|
|
foreach (var annotatingUser in annotatingUsers)
|
149 |
|
|
{
|
150 |
|
|
var annotation = databaseContext.Annotations
|
151 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
152 |
3cee56d3
|
Vojtěch Bartička
|
.Include(a => a.Document)
|
153 |
8c9ce202
|
Vojtěch Bartička
|
.Single(a => a.Document == document && a.User == annotatingUser && !(a is FinalAnnotation));
|
154 |
3cee56d3
|
Vojtěch Bartička
|
var dui = mapper.Map<DocumentUserInfo>(annotatingUser);
|
155 |
|
|
dui.State = annotation.State;
|
156 |
|
|
annotatingUsersDto.Add(dui);
|
157 |
|
|
}
|
158 |
5a08541d
|
Vojtěch Bartička
|
|
159 |
85b8834f
|
Vojtěch Bartička
|
DocumentListInfo dli = mapper.Map<DocumentListInfo>(document);
|
160 |
|
|
dli.AnnotatingUsers = annotatingUsersDto;
|
161 |
|
|
|
162 |
|
|
dli.FinalizedExists = databaseContext.FinalAnnotations.Any(fa => fa.Document == document);
|
163 |
|
|
if (dli.FinalizedExists)
|
164 |
|
|
{
|
165 |
|
|
var finalizedAnnotation = databaseContext.FinalAnnotations
|
166 |
|
|
.Include(fa => fa.Annotations)
|
167 |
|
|
.ThenInclude(a => a.User)
|
168 |
|
|
.Single(fa => fa.Document == document);
|
169 |
4ed6b05c
|
Vojtěch Bartička
|
|
170 |
|
|
dli.FinalizedAnnotationId = finalizedAnnotation.Id;
|
171 |
85b8834f
|
Vojtěch Bartička
|
dli.FinalizedState = finalizedAnnotation.State;
|
172 |
|
|
dli.FinalAnnotations = new();
|
173 |
|
|
|
174 |
|
|
foreach (var annotation in finalizedAnnotation.Annotations)
|
175 |
|
|
{
|
176 |
|
|
dli.FinalAnnotations.Add(new()
|
177 |
|
|
{
|
178 |
|
|
AnnotationId = annotation.Id,
|
179 |
|
|
UserId = annotation.User.Id,
|
180 |
|
|
Username = annotation.User.Username,
|
181 |
|
|
UserFirstName = annotation.User.Name,
|
182 |
4ed6b05c
|
Vojtěch Bartička
|
UserSurname = annotation.User.Surname,
|
183 |
85b8834f
|
Vojtěch Bartička
|
});
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
documentInfos.Add(dli);
|
189 |
24e1c89d
|
Vojtěch Bartička
|
}
|
190 |
|
|
|
191 |
|
|
return new DocumentListResponse()
|
192 |
|
|
{
|
193 |
|
|
PageCount = pageCount,
|
194 |
42c654f6
|
Vojtěch Bartička
|
PageIndex = pageIndex,
|
195 |
24e1c89d
|
Vojtěch Bartička
|
TotalCount = totalCount,
|
196 |
97442c93
|
Vojtěch Bartička
|
Documents = documentInfos.OrderBy(di => di.Name).ToList();
|
197 |
24e1c89d
|
Vojtěch Bartička
|
};
|
198 |
|
|
}
|
199 |
|
|
|
200 |
5adba4c4
|
Vojtěch Bartička
|
public void SetRequiredAnnotationsForDocuments(SetRequiredAnnotationsRequest request)
|
201 |
|
|
{
|
202 |
|
|
var documents = databaseContext.Documents.Where(d => request.DocumentIds.Contains(d.Id));
|
203 |
|
|
foreach (var doc in documents)
|
204 |
|
|
{
|
205 |
|
|
doc.RequiredAnnotations = request.RequiredAnnotations;
|
206 |
|
|
}
|
207 |
|
|
databaseContext.SaveChanges();
|
208 |
|
|
}
|
209 |
0a6d22b7
|
Vojtěch Bartička
|
|
210 |
|
|
public void SetRequiredAnnotationsGlobal(int requiredAnnotations)
|
211 |
|
|
{
|
212 |
ceb95b98
|
Vojtěch Bartička
|
var requiredAnnotationsItem = databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey);
|
213 |
|
|
requiredAnnotationsItem.Value = requiredAnnotations.ToString();
|
214 |
|
|
databaseContext.SaveChanges();
|
215 |
0a6d22b7
|
Vojtěch Bartička
|
}
|
216 |
|
|
|
217 |
ceb95b98
|
Vojtěch Bartička
|
public int GetRequiredAnnotationsGlobal()
|
218 |
|
|
{
|
219 |
|
|
var requiredAnnotationsItem = databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey);
|
220 |
|
|
return int.Parse(requiredAnnotationsItem.Value);
|
221 |
3ea0ef00
|
Vojtěch Bartička
|
}
|
222 |
|
|
|
223 |
42bb0025
|
Vojtěch Bartička
|
public DocumentPreviewResponse GetDocumentPreview(Guid documentId)
|
224 |
|
|
{
|
225 |
|
|
try
|
226 |
|
|
{
|
227 |
|
|
var document = databaseContext.Documents.Include(d => d.Content).Single(d => d.Id == documentId);
|
228 |
|
|
HtmlSanitizer sanitizer = new HtmlSanitizer();
|
229 |
343aa66b
|
Vojtěch Bartička
|
sanitizer.AllowedAttributes.Clear();
|
230 |
|
|
if (sanitizer.AllowedTags.Contains("img"))
|
231 |
|
|
{
|
232 |
|
|
sanitizer.AllowedTags.Remove("img");
|
233 |
|
|
}
|
234 |
42bb0025
|
Vojtěch Bartička
|
return new DocumentPreviewResponse()
|
235 |
|
|
{
|
236 |
|
|
Content = sanitizer.Sanitize(document.Content.Content)
|
237 |
|
|
};
|
238 |
|
|
}
|
239 |
|
|
catch (InvalidOperationException e)
|
240 |
|
|
{
|
241 |
|
|
throw new InvalidOperationException("Document not found");
|
242 |
|
|
}
|
243 |
|
|
}
|
244 |
24e1c89d
|
Vojtěch Bartička
|
}
|
245 |
|
|
}
|