Projekt

Obecné

Profil

Stáhnout (4.98 KB) Statistiky
| Větev: | Tag: | Revize:
1
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

    
13
namespace Core.Services.DocumentService
14
{
15
    public class DocumentServiceEF : IDocumentService
16
    {
17
        private readonly DatabaseContext databaseContext;
18
        private readonly ILogger logger;
19

    
20
        public DocumentServiceEF(DatabaseContext databaseContext, ILogger logger)
21
        {
22
            this.databaseContext = databaseContext;
23
            this.logger = logger;
24
        }
25

    
26
        /// <summary>
27
        /// Adds the documents in request to the database
28
        /// </summary>
29
        /// <param name="request">request</param>
30
        /// <param name="userId">GUID of the user</param>
31
        /// <exception cref="InvalidOperationException">No User with passed GUID</exception>
32
        /// <exception cref="FormatException">Error decoding BASE64 content</exception>
33
        /// <exception cref="InvalidDataException">Zip file has wrong format</exception>
34
        /// <exception cref="IOException">Error reading entries from the zip file</exception>
35
        public void AddDocuments(DocumentAddRequest request, Guid userId)
36
        {
37
            User user = databaseContext.Users.Single(u => u.Id == userId);
38

    
39
            foreach (var documentInfo in request.Documents)
40
            {
41
                if (documentInfo.Format == Models.Enums.EAddDocumentFormat.PLAINTEXT)
42
                {
43
                    SaveDocument(documentInfo.Content, user, documentInfo.Name);
44
                }
45
                else if (documentInfo.Format == Models.Enums.EAddDocumentFormat.BASE64)
46
                {
47
                    var (names, contents) = UnzipDocuments(documentInfo.Content);
48
                    for (int i = 0; i < names.Count; i++)
49
                    {
50
                        SaveDocument(contents[i], user, names[i]);
51
                    }
52
                }
53
            }
54
        }
55

    
56
        /// <summary>
57
        /// 
58
        /// </summary>
59
        /// <param name="base64encoded"></param>
60
        /// <returns></returns>
61
        private (List<string> Names, List<string> Contents) UnzipDocuments(string base64encoded)
62
        {
63
            List<string> names = new();
64
            List<string> contents = new();
65

    
66
            byte[] decoded = Convert.FromBase64String(base64encoded);
67

    
68
            using (var zipStream = new MemoryStream(decoded))
69
            using (var zipArchive = new ZipArchive(zipStream))
70
            {
71
                foreach (var entry in zipArchive.Entries)
72
                {
73
                    names.Add(entry.Name);
74
                    using (var streamReader = new StreamReader(entry.Open()))
75
                    {
76
                        string text = streamReader.ReadToEnd();
77
                        contents.Add(text);
78
                    }
79
                }
80
            }
81

    
82
            return (names, contents);
83
        }
84

    
85
        private void SaveDocument(string content, User userAdded, string documentName)
86
        {
87
            DocumentContent documentContent = new DocumentContent()
88
            {
89
                Content = content
90
            };
91

    
92
            Document document = new Document()
93
            {
94
                DateAdded = DateTime.Now,
95
                Content = documentContent,
96
                UserAdded = userAdded,
97
                Name = documentName,
98
                Length = documentContent.Content.Length,
99
                RequiredAnnotations = 3                     // TODO this is only for beta testing purposes
100
            };
101

    
102
            databaseContext.DocumentContents.Add(documentContent);
103
            databaseContext.Documents.Add(document);
104
            databaseContext.SaveChanges();                  // Maybe do this after all the documents are added
105
        }
106

    
107
        public DocumentListResponse GetDocuments(DocumentListRequest request)
108
        {
109
            var firstIndex = request.PageIndex * request.PageSize;
110
            var documents = databaseContext.Documents.Select(d => d).ToList();
111
            var totalCount = documents.Count;
112
            var pageCount = totalCount / request.PageSize;
113
            if (pageCount == 0 && totalCount > 0)
114
            {
115
                pageCount = 1;
116
            }
117

    
118
            List<DocumentListInfo> documentInfos = new List<DocumentListInfo>();
119
            foreach (var document in documents.GetRange(firstIndex, request.PageSize))
120
            {
121
                documentInfos.Add(new DocumentListInfo()
122
                {
123
                    Id = document.Id,
124
                    Length = document.Length,
125
                    Name = document.Name,
126
                    RequiredAnnotations = document.RequiredAnnotations
127
                });
128
            }
129

    
130
            return new DocumentListResponse()
131
            {
132
                PageCount = pageCount,
133
                PageIndex = request.PageIndex,
134
                TotalCount = totalCount,
135
                Documents = documentInfos
136
            };
137
        }
138

    
139
    }
140
}
(1-1/2)