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;
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 case FilePurpose.InternalTransfer:
107 if (string.IsNullOrEmpty(Settings.InternalTransferFolder) || string.IsNullOrEmpty(Settings.InternalTransferRoot))
108 return CanUploadResult.FileQuotaReached;
109 break;
110 }
111
112 this.fileCount++;
113 this.byteCount += Size;
114
115 return 0;
116 }
117 }
118
127 public bool TryGetSpecialFile(string FileName, string FileContentType, long FileSize, out SpecialFile Result)
128 {
129 lock (this.synchObject)
130 {
131 if (!(this.specialFiles is null))
132 {
133 foreach (SpecialFile Rec in this.specialFiles)
134 {
135 if (Rec.FileName == FileName && Rec.FileContentType == FileContentType && Rec.FileSize == FileSize)
136 {
137 Result = Rec;
138 return true;
139 }
140 }
141 }
142 }
143
144 Result = null;
145 return false;
146 }
147
155 public void AddSpecialFile(string FileName, string FileContentType, long FileSize, FilePurpose Purpose)
156 {
157 lock (this.synchObject)
158 {
159 if (!(this.specialFiles is null))
160 {
161 foreach (SpecialFile Rec in this.specialFiles)
162 {
163 if (Rec.FileName == FileName && Rec.FileContentType == FileContentType && Rec.FileSize == FileSize)
164 {
165 Rec.Purpose = Purpose;
166 return;
167 }
168 }
169 }
170
171 if (this.specialFiles is null)
172 this.specialFiles = new LinkedList<SpecialFile>();
173
174 this.specialFiles.AddLast(new SpecialFile(Purpose, FileName, FileContentType, FileSize));
175 }
176 }
177
185 public bool RemoveSpecialFile(string FileName, string FileContentType, long FileSize)
186 {
187 lock (this.synchObject)
188 {
189 if (!(this.specialFiles is null))
190 {
191 LinkedListNode<SpecialFile> Loop = this.specialFiles.First;
192
193 while (!(Loop is null) &&
194 (Loop.Value.FileName != FileName ||
195 Loop.Value.FileContentType != FileContentType ||
196 Loop.Value.FileSize != FileSize))
197 {
198 Loop = Loop.Next;
199 }
200
201 if (!(Loop is null))
202 {
203 this.specialFiles.Remove(Loop);
204 return true;
205 }
206 }
207
208 return false;
209 }
210 }
211
212 }
213}
Represents a case-insensitive string.
FilePurpose
Purpose of file uploaded
Definition: FileInfo.cs:9