Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UploadInformation.cs
1using System;
2using System.Collections.Generic;
4
6{
10 internal enum CanUploadResult
11 {
15 Permitted,
16
20 FileQuotaReached,
21
25 ByteQuotaReached
26 }
27
31 internal class UploadInformation
32 {
33 private readonly CaseInsensitiveString bareJid;
34 private long byteCount;
35 private int fileCount;
36 private DateTime startedAt;
37 private readonly object synchObject;
38 private LinkedList<SpecialFile> specialFiles;
39
44 public UploadInformation(CaseInsensitiveString BareJid)
45 {
46 this.bareJid = BareJid;
47 this.byteCount = 0;
48 this.fileCount = 0;
49 this.startedAt = DateTime.MinValue;
50 this.synchObject = new object();
51 this.specialFiles = null;
52 }
53
57 public CaseInsensitiveString BareJid => this.bareJid;
58
66 public CanUploadResult CanUploadFile(long Size, FilePurpose Purpose, HttpFileUploadSettings Settings)
67 {
68 lock (this.synchObject)
69 {
70 switch (Purpose)
71 {
72 case FilePurpose.Temporary:
73 DateTime Now = DateTime.Now;
74 TimeSpan TS = Now - this.startedAt;
75
76 if (TS.TotalMinutes >= 1.0)
77 {
78 this.startedAt = Now;
79 this.byteCount = 0;
80 this.fileCount = 0;
81 }
82
83 if (this.fileCount >= Settings.MaxFilesPerMinute)
84 return CanUploadResult.FileQuotaReached;
85
86 if (this.byteCount + Size > Settings.MaxBytesPerMinute)
87 return CanUploadResult.ByteQuotaReached;
88
89 break;
90
91 case FilePurpose.Backup:
92 if (string.IsNullOrEmpty(Settings.KeyFolder) || string.IsNullOrEmpty(Settings.BackupFolder))
93 return CanUploadResult.FileQuotaReached;
94 break;
95
96 case FilePurpose.Encrypted:
97 if (string.IsNullOrEmpty(Settings.EncryptedStorageFolder) || string.IsNullOrEmpty(Settings.EncryptedStorageRoot))
98 return CanUploadResult.FileQuotaReached;
99 break;
100
101 case FilePurpose.PubSub:
102 if (string.IsNullOrEmpty(Settings.PubSubStorageFolder) || string.IsNullOrEmpty(Settings.PubSubStorageRoot))
103 return CanUploadResult.FileQuotaReached;
104 break;
105 }
106
107 this.fileCount++;
108 this.byteCount += Size;
109
110 return 0;
111 }
112 }
113
122 public bool TryGetSpecialFile(string FileName, string FileContentType, long FileSize, out SpecialFile Result)
123 {
124 lock (this.synchObject)
125 {
126 if (!(this.specialFiles is null))
127 {
128 foreach (SpecialFile Rec in this.specialFiles)
129 {
130 if (Rec.FileName == FileName && Rec.FileContentType == FileContentType && Rec.FileSize == FileSize)
131 {
132 Result = Rec;
133 return true;
134 }
135 }
136 }
137 }
138
139 Result = null;
140 return false;
141 }
142
150 public void AddSpecialFile(string FileName, string FileContentType, long FileSize, FilePurpose Purpose)
151 {
152 lock (this.synchObject)
153 {
154 if (!(this.specialFiles is null))
155 {
156 foreach (SpecialFile Rec in this.specialFiles)
157 {
158 if (Rec.FileName == FileName && Rec.FileContentType == FileContentType && Rec.FileSize == FileSize)
159 {
160 Rec.Purpose = Purpose;
161 return;
162 }
163 }
164 }
165
166 if (this.specialFiles is null)
167 this.specialFiles = new LinkedList<SpecialFile>();
168
169 this.specialFiles.AddLast(new SpecialFile(Purpose, FileName, FileContentType, FileSize));
170 }
171 }
172
180 public bool RemoveSpecialFile(string FileName, string FileContentType, long FileSize)
181 {
182 lock (this.synchObject)
183 {
184 if (!(this.specialFiles is null))
185 {
186 LinkedListNode<SpecialFile> Loop = this.specialFiles.First;
187
188 while (!(Loop is null) &&
189 (Loop.Value.FileName != FileName ||
190 Loop.Value.FileContentType != FileContentType ||
191 Loop.Value.FileSize != FileSize))
192 {
193 Loop = Loop.Next;
194 }
195
196 if (!(Loop is null))
197 {
198 this.specialFiles.Remove(Loop);
199 return true;
200 }
201 }
202
203 return false;
204 }
205 }
206
207 }
208}
Represents a case-insensitive string.
FilePurpose
Purpose of file uploaded
Definition: FileInfo.cs:9