/
PtfkFileInfo.cs
769 lines (710 loc) · 35.6 KB
/
PtfkFileInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Petaframework.Interfaces;
using Petaframework.Strict;
using PetaframeworkStd.Exceptions;
using PetaframeworkStd.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using static Petaframework.Settings;
namespace Petaframework
{
/// <summary>
/// Class that used for File Upload
/// </summary>
[Serializable]
[NotMapped]
public class PtfkFileInfo
{
private static string _FUP_SESSION_NAME = "_PTFK_UserFiles_";
private static Boolean _UsePtfkMediaEngine { get { return BusinessClassMedia != null; } }
private const int DELETE_FILES_AFTER = 1;
internal static object BusinessClassMedia { get; set; }
public String UID { get; set; }
[JsonIgnore]
public String OwnerID { get; set; }
[JsonIgnore]
public System.IO.FileInfo FileInfo { get; set; }
public String EntityName { get; set; }
[JsonIgnore]
public long ParentID { get; set; }
public String Name { get; set; }
public String EntityProperty { get; set; }
public String ExternalInfo { get; set; }
[JsonIgnore]
public Boolean ToDelete { get; set; } = false;
[JsonIgnore]
private string _hash = string.Empty;
[JsonIgnore]
public string Hash
{
get
{
if (!string.IsNullOrWhiteSpace(_hash) || !FileInfo.Exists)
return _hash;
_hash = Tools.GetHashMD5(FileInfo);
return _hash;
}
set
{
_hash = value;
}
}
[JsonIgnore]
public long MediaID { get; set; } = 0;
[JsonIgnore]
public System.Threading.Tasks.Task<Object> GenericArg { get; set; }
private static DirectoryInfo di;
public static String GetServerPath(String ownerID)
{
return "~/_Temp/" + ownerID + "/";
}
public static DirectoryInfo GetDirectoryInfo(IPtfkSession session)
{
di = new DirectoryInfo(Server.MapPath(GetServerPath(session.Login)));
if (!di.Exists)
di.Create();
else
{
foreach (var item in di.GetFiles().Where(x => x.LastWriteTimeUtc <= DateTime.Now.AddHours(-DELETE_FILES_AFTER)))
{
try
{
item.Delete();
}
catch (Exception ex) { }
}
}
return di;
}
public static List<PtfkFileInfo> MaterializeFiles(
IPtfkForm entity,
string entityProperty)
{
return PtfkFileInfo.GetFilesPrivate(entity.GetOwner(), entity.GetType().Name, entityProperty, entity.Id, entity, (ILogger)null);
}
private static List<PtfkFileInfo> GetFilesPrivate(
IPtfkSession session,
string entityName,
string entityProperty,
long entityID,
IPtfkForm form = null,
ILogger _logger = null)
{
List<PtfkFileInfo> files = PtfkFileInfo.GetFiles(session);
EventId id = new Random().Next();
_logger?.Log<PtfkFileInfo>(LogLevel.Information, id, null, new Exception(Tools.ToJson(new
{
AllInMemory = files,
Owner = session?.Current?.Login
}, true, false)), null);
List<PtfkFileInfo> list1 = files.Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && (x.ParentID.Equals(0) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty))) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
List<PtfkFileInfo> list2 = files.Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && (x.ParentID.Equals(entityID) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty))) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
IPtfkEntity entityByClassName = Tools.GetIPtfkEntityByClassName(session.GetType().Assembly, entityName, session, (ILogger)null);
if (entityByClassName == null)
{
PtfkFileInfo.LogInfo(113, _logger, session, id);
if (form != null && list1.Count<PtfkFileInfo>() > 0 && list2.Count<PtfkFileInfo>() == 0)
{
files.ForEach((System.Action<PtfkFileInfo>)(x => x.FileInfo = new FileInfo(x.FileInfo.FullName)));
list2 = files.Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && !x.ToDelete && (x.ParentID.Equals(entityID) || x.ParentID.Equals(0)) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty)) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
}
return list2;
}
PropertyInfo property1 = entityByClassName.GetType().GetProperty(entityProperty);
Type type1 = Nullable.GetUnderlyingType(property1.PropertyType);
if ((object)type1 == null)
type1 = property1.PropertyType;
Type t1 = type1;
if (entityProperty.StartsWith(Constants.EntityPtfkFileInfoPrefix))
{
PtfkFileInfo.LogInfo(128, _logger, session, id);
if (form != null && list1.Count<PtfkFileInfo>() > 0 && list2.Count<PtfkFileInfo>() == 0)
{
PtfkFileInfo.LogInfo(131, _logger, session, id);
files.ForEach((System.Action<PtfkFileInfo>)(x => x.FileInfo = new FileInfo(x.FileInfo.FullName)));
list2 = files.Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && !x.ToDelete && (x.ParentID.Equals(entityID) || x.ParentID.Equals(0)) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty)) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
}
return PtfkFileInfo.GetUniqueFiles(list2, t1);
}
try
{
PtfkFileInfo.LogInfo(141, _logger, session, id);
entityProperty = Constants.EntityPtfkFileInfoPrefix + entityProperty;
list2 = PtfkFileInfo.GetFiles(session).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && (x.ParentID.Equals(entityID) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty))) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
if (form != null && list1.Count<PtfkFileInfo>() > 0 && list2.Count<PtfkFileInfo>() == 0)
{
PtfkFileInfo.LogInfo(146, _logger, session, id);
files.ForEach((System.Action<PtfkFileInfo>)(x => x.FileInfo = new FileInfo(x.FileInfo.FullName)));
list2 = files.Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.EntityName.Equals(entityName) && !x.ToDelete && (x.ParentID.Equals(entityID) || x.ParentID.Equals(0)) && (x.EntityProperty.Equals(entityProperty) || x.EntityProperty.EndsWith("_" + entityProperty) || entityProperty.EndsWith("_" + x.EntityProperty)) && x.FileInfo.Exists)).ToList<PtfkFileInfo>();
}
PropertyInfo property2 = entityByClassName.GetType().GetProperty(entityProperty);
Type type2 = Nullable.GetUnderlyingType(property2.PropertyType);
if ((object)type2 == null)
type2 = property2.PropertyType;
Type t2 = type2;
return PtfkFileInfo.GetUniqueFiles(list2, t2);
}
catch (Exception ex)
{
PtfkFileInfo.LogError(131, _logger, session, id, ex);
return list2;
}
}
private static void LogInfo(int line, ILogger _logger, IPtfkSession session, EventId id)
{
_logger?.Log<PtfkFileInfo>(LogLevel.Information, id, null, new Exception(Tools.ToJson((object)new
{
Line = line,
Owner = session?.Current?.Login
}, true, false)), null);
}
private static void LogError(
int line,
ILogger _logger,
IPtfkSession session,
EventId id,
Exception ex)
{
_logger?.Log<PtfkFileInfo>(LogLevel.Error, id, null, new Exception(Tools.ToJson((object)new
{
Line = line,
Owner = session?.Current?.Login,
Message = ex.Message,
InnerException = ex.InnerException,
StackTrace = ex.StackTrace
}, true, false)), null);
}
private static List<PtfkFileInfo> GetUniqueFiles(
List<PtfkFileInfo> docs,
Type t)
{
if (docs.Count() == 0)
return docs;
if (Tools.DoesTypeWereSimilar(t, typeof(PtfkFileInfo)))
return new List<PtfkFileInfo>()
{
docs.OrderBy(x => x.FileInfo.LastWriteTime).LastOrDefault()
};
if (!Tools.DoesTypeWereSimilar(t, typeof(ICollection<PtfkFileInfo>)))
return docs;
List<PtfkFileInfo> source1 = new List<PtfkFileInfo>();
foreach (PtfkFileInfo doc in docs)
{
PtfkFileInfo d = doc;
IEnumerable<PtfkFileInfo> source2 = source1.Where(x => x.Hash.Equals(d.Hash));
if (source2.Count<PtfkFileInfo>() == 0)
source1.Add(d);
else
source1.Add(source2.OrderBy(x => x.FileInfo.LastWriteTime).LastOrDefault());
}
return source1;
}
public static List<PtfkFileInfo> GetFiles(
IPtfkSession session,
string entityName,
string entityProperty,
long entityID,
ILogger _logger = null)
{
return PtfkFileInfo.GetFilesPrivate(session, entityName, entityProperty, entityID, (IPtfkForm)null, _logger);
}
internal static List<PtfkFileInfo> GetFiles(IPtfkSession session)
{
lock (PtfkFileInfo._FUP_SESSION_NAME)
{
object obj = PtfkFileInfo.Get(session);
List<PtfkFileInfo> source = new List<PtfkFileInfo>();
try
{
if (obj != null)
source = ((IEnumerable<PtfkFileInfo>)obj).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.OwnerID.Equals(session.Login))).ToList<PtfkFileInfo>();
}
catch (Exception ex)
{
}
finally
{
if (source.Count<PtfkFileInfo>() == 0)
{
FileInfo[] files = ((IEnumerable<FileInfo>)PtfkFileInfo.GetDirectoryInfo(session).GetFiles("*.*", SearchOption.AllDirectories)).Where<FileInfo>((Func<FileInfo, bool>)(x => x.LastWriteTimeUtc <= DateTime.UtcNow.AddHours(-1.0))).ToArray<FileInfo>();
new Thread((ThreadStart)(() => PtfkFileInfo.DeleteFiles(files))).Start();
}
}
source.ForEach((System.Action<PtfkFileInfo>)(x => x.FileInfo = new FileInfo(x.FileInfo.FullName)));
return source;
}
}
internal static void DeleteFiles(FileInfo[] files)
{
foreach (FileInfo file in files)
{
try
{
file.Delete();
}
catch (Exception ex)
{
}
}
}
private static object Get(IPtfkSession owner)
{
try
{
if (Current.Session == null)
PtfkFileInfo.Set(owner, (object)new List<PtfkFileInfo>());
if (!String.IsNullOrWhiteSpace(owner.Login))
return Current.Session[PtfkFileInfo._FUP_SESSION_NAME][owner.Login];
return null;
}
catch (Exception ex)
{
PtfkFileInfo.Set(owner, (object)new List<PtfkFileInfo>());
return PtfkFileInfo.Get(owner);
}
}
private static void Set(IPtfkSession owner, object filesList)
{
if (Current.Session == null)
Current.Session = new Dictionary<string, Dictionary<string, object>>();
if (!Current.Session.ContainsKey(PtfkFileInfo._FUP_SESSION_NAME))
Current.Session[PtfkFileInfo._FUP_SESSION_NAME] = new Dictionary<string, object>();
if (!String.IsNullOrWhiteSpace(owner.Login))
Current.Session[PtfkFileInfo._FUP_SESSION_NAME][owner.Login] = filesList;
}
private static PtfkFileInfo UploadFilePrivate<T>(
T form,
string propertyName,
string fileExtension,
byte[] fileContent,
string fileName,
string externalInfo = null)
where T : class, IPtfkForm, IPtfkEntity
{
FileInfo fileInfo = new FileInfo(Path.Combine(PtfkFileInfo.GetDirectoryInfo(form.Owner.Current).FullName, propertyName + (fileExtension.StartsWith(".") ? "" : ".") + fileExtension));
File.WriteAllBytes(fileInfo.FullName, fileContent);
PtfkFileInfo file = new PtfkFileInfo()
{
FileInfo = fileInfo,
OwnerID = form.Owner.Current.Login,
UID = Guid.NewGuid().ToString(),
EntityName = form.GetType().Name,
ParentID = ((IPtfkEntity)form).Id,
Name = fileInfo.Name,
EntityProperty = propertyName,
ExternalInfo = externalInfo
};
string mirroredOf = form.GetMirroredOf(propertyName);
if (mirroredOf != null)
{
PropertyInfo property = form.GetType().GetProperty(mirroredOf);
if (property != (PropertyInfo)null && property.PropertyType == typeof(string))
{
string json = property.GetValue((object)form)?.ToString();
IList<string> stringList = (IList<string>)null;
if (string.IsNullOrWhiteSpace(json))
stringList = (IList<string>)new List<string>();
else if (Tools.IsJsonArray(json))
stringList = Tools.FromJson<IList<string>>(json, false);
stringList.Add(file.Hash);
property.SetValue((object)form, (object)Tools.ToJson((object)stringList));
PtfkMedia ptfkMedia = new PtfkMedia();
ptfkMedia.Bytes = File.ReadAllBytes(file.FileInfo.FullName);
ptfkMedia.EntityName = file.EntityName;
ptfkMedia.Extension = file.FileInfo.Extension;
ptfkMedia.Hash = file.Hash;
ptfkMedia.Name = string.IsNullOrWhiteSpace(fileName) ? file.Name : fileName;
ptfkMedia.Path = "*";
ptfkMedia.Size = file.FileInfo.Length;
ptfkMedia.EntityProperty = propertyName;
ptfkMedia.ExternalInfo = externalInfo;
try
{
((object)form as PtfkForm<T>).CurrentGenerator.CurrentPageConfig.CurrDForm.MediaFiles.Add((IPtfkMedia)ptfkMedia);
}
catch (Exception ex)
{
throw new FormConfigurationException("No has found configuration informations to this Form.");
}
}
}
PtfkFileInfo.AddFile(form.Owner, file, (ILogger)null);
return file;
}
/// <summary>
/// Uploads a user's file to the file persistence environment.
/// </summary>
/// <param name="form">The form that contains the user's file</param>
/// <param name="propertyName">The property name that contains de user's file on the form</param>
/// <param name="fileExtension">The file's extension</param>
/// <param name="fileContent">The file's content</param>
/// <returns>PtfkFileInfo</returns>
public static PtfkFileInfo UploadFile<T>(
T form,
string propertyName,
string fileExtension,
FileInfo fileInfo)
where T : class, IPtfkForm, IPtfkEntity
{
return PtfkFileInfo.UploadFilePrivate<T>(form, propertyName, fileExtension, File.ReadAllBytes(fileInfo.FullName), fileInfo.Name, (string)null);
}
/// <summary>
/// Uploads a user's file to the file persistence environment.
/// </summary>
/// <param name="form">The form that contains the user's file</param>
/// <param name="propertyName">The property name that contains de user's file on the form</param>
/// <param name="fileExtension">The file's extension</param>
/// <param name="fileContent">The file's content</param>
/// <param name="fileName">File Name</param>
/// <param name="externalInfo">External Info to Persist</param>
/// <returns>PtfkFileInfo</returns>
public static PtfkFileInfo UploadFile<T>(
T form,
string propertyName,
string fileExtension,
byte[] fileContent,
string fileName = null,
string externalInfo = null)
where T : class, IPtfkForm, IPtfkEntity
{
return PtfkFileInfo.UploadFilePrivate<T>(form, propertyName, fileExtension, fileContent, fileName, externalInfo);
}
public static void AddFile(IPtfkSession session, PtfkFileInfo file, ILogger _logger = null)
{
lock (PtfkFileInfo._FUP_SESSION_NAME)
{
List<PtfkFileInfo> ptfkFileInfoList = (List<PtfkFileInfo>)PtfkFileInfo.Get(session);
if (ptfkFileInfoList == null)
{
ptfkFileInfoList = new List<PtfkFileInfo>();
ptfkFileInfoList.Add(file);
}
else
{
int index = ptfkFileInfoList.FindIndex((Predicate<PtfkFileInfo>)(x => x.UID.Equals(file.UID) && (x.ParentID.Equals(file.ParentID) && x.OwnerID.Equals(file.OwnerID)) && x.EntityProperty.Equals(file.EntityProperty)));
if (index >= 0)
{
if (ptfkFileInfoList[index].ToDelete)
file.ToDelete = true;
ptfkFileInfoList[index] = file;
}
else
ptfkFileInfoList.Add(file);
}
PtfkFileInfo.Set(session, (object)ptfkFileInfoList);
Assembly assembly = PtfkFileInfo.BusinessClassMedia.GetType().Assembly;
string entityName = file.EntityName;
PrivatePtfkSession privatePtfkSession = new PrivatePtfkSession();
privatePtfkSession.Login = file.OwnerID;
ILogger _logger1 = _logger;
Tools.GetIPtfkEntityByClassName(assembly, entityName, (IPtfkSession)privatePtfkSession, _logger1).OnFileUploaded(new PtfkEventArgs<PtfkFileInfo>(ref file));
}
}
public static void ClearFiles(string entityName, IPtfkSession session, params long[] parentID)
{
foreach (long num in ((IEnumerable<long>)parentID).Distinct<long>())
{
long lID = 0;
long.TryParse(num.ToString(), out lID);
lock (PtfkFileInfo._FUP_SESSION_NAME)
{
if (lID <= 0)
{
var includedList = GetFiles(session).Where(x => !x.OwnerID.Equals(session.Login) &&
!x.EntityName.Equals(entityName)
).ToList();
PtfkFileInfo.Set(session, (object)PtfkFileInfo.GetFiles(session).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => !includedList.Contains(x))).ToList<PtfkFileInfo>());
}
else
{
List<PtfkFileInfo> includedList = PtfkFileInfo.GetFiles(session).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => !x.OwnerID.Equals(session.Login) && !x.ParentID.Equals(lID) && !x.EntityName.Equals(entityName))).ToList<PtfkFileInfo>();
PtfkFileInfo.Set(session, (object)PtfkFileInfo.GetFiles(session).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => !includedList.Contains(x))).ToList<PtfkFileInfo>());
}
}
}
}
private static void RemoveFile(IPtfkSession session, PtfkFileInfo file)
{
List<PtfkFileInfo> ptfkFileInfoList = (List<PtfkFileInfo>)PtfkFileInfo.Get(session);
ptfkFileInfoList?.Remove(file);
PtfkFileInfo.Set(session, (object)ptfkFileInfoList);
file.FileInfo.Delete();
}
public static void DeleteFile(IPtfkSession session, string qquuid)
{
lock (PtfkFileInfo._FUP_SESSION_NAME)
{
object obj = PtfkFileInfo.Get(session);
PtfkFileInfo file = obj == null ? new PtfkFileInfo() : ((IEnumerable<PtfkFileInfo>)obj).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.UID.Equals(qquuid))).FirstOrDefault<PtfkFileInfo>();
try
{
if (PtfkFileInfo.IsNewFile(file))
{
PtfkFileInfo.RemoveFile(session, file);
}
else
{
file.ToDelete = true;
PtfkFileInfo.AddFile(session, file, (ILogger)null);
}
}
catch (Exception ex)
{
}
}
}
internal static void InitializeBusinessClass(IPtfkForm entity, IPtfkSession session)
{
BusinessClassMedia = Tools.GetIBusinessMediaClass(entity.GetType().Assembly, session.Login);
}
/// <summary>
/// Loads entity files to the user interface environment
/// </summary>
/// <param name="entity">Entity that has the files for loading</param>
/// <param name="propertyName">Nome da propriedade da entidade que contém o arquivo</param>
/// <param name="session">Then Owner Session that will view the file</param>
public static void LoadFilesFromEntity(IPtfkForm entity, String propertyName, IPtfkSession session)
{
lock (_FUP_SESSION_NAME)
{
InitializeBusinessClass(entity, session);
var ID = Convert.ToInt64(entity.GetType().GetProperty(entity.GetIdAttributeName()).GetValue(entity));
var obj = entity.GetType().GetProperty(propertyName).GetValue(entity);
if (ID > 0 && obj != null)
{
if (!_UsePtfkMediaEngine)
{
MaterializeFile(session, ID, obj as byte[], entity, propertyName);
}
else
{
List<String> hashList = new List<string>();
try
{
hashList = JsonConvert.DeserializeObject<List<String>>(obj as String);
var instance = Activator.CreateInstance(BusinessClassMedia.GetType(), new object[] { session });
var method = BusinessClassMedia.GetType().GetMethods().Where(x => x.Name.Equals(nameof(IPtfkBusiness<object>.List)) && x.GetParameters().Length == 0).FirstOrDefault();
var list = method.Invoke(BusinessClassMedia, null) as IQueryable<IPtfkMedia>;
var medias = from m in list
where m.EntityId.Equals(ID) && m.EntityName.Equals(entity.GetType().Name) &&
string.IsNullOrWhiteSpace(m.IsDeath) &&
!string.IsNullOrWhiteSpace(m.Hash) && hashList.Contains(m.Hash)
select m;
foreach (var item in medias)
{
switch (StorageMode)
{
case StorageMode.OnContentDB:
var filePath = GetStorageRelativeFilePath(entity, propertyName, item);
var file = new FileInfo(Path.Combine(Settings.Storage.Path, filePath));
if (file.Exists)
MaterializeFile(session, ID, File.ReadAllBytes(file.FullName), entity, propertyName, item);
else if (!file.Exists && item.Bytes?.Length > 0)
MaterializeFile(session, ID, item.Bytes, entity, propertyName, item);
else
throw new FileNotFoundOnStorageException(filePath);
break;
default:
MaterializeFile(session, ID, item.Bytes, entity, propertyName, item);
break;
}
}
}
catch (Exception ex)
{
}
}
}
}
}
internal static String GetStorageAbsoluteFilePath(IPtfkForm entity, String propertyName, IPtfkMedia media, bool toSaveName = false)
{
switch (StorageMode)
{
case StorageMode.OnContentDB:
return Path.Combine(Settings.Storage.Path, GetStorageRelativeFilePath(entity, propertyName, media, toSaveName));
break;
default:
return string.Empty;
break;
}
}
internal static String GetStorageRelativeFilePath(IPtfkForm entity, String propertyName, IPtfkMedia media, bool toSaveName = false)
{
var name = Guid.NewGuid().ToString() + media.Extension;
if (!toSaveName && media?.Path.Trim() != Constants.OnStorageDBMSFlag && !String.IsNullOrWhiteSpace(media?.Path))
return media.Path;
if (toSaveName && StorageMode == StorageMode.OnContentDB && media.Hash == Tools.GetHashMD5(media.Bytes))
if (media.Path.Trim() != Constants.OnStorageDBMSFlag)
return media.Path;
else
{
var di = new DirectoryInfo(Path.Combine(Settings.Storage.Path, GetStorageFolderPath(entity, propertyName)));
if (di.Exists)
{
var f = Directory.GetFiles(di.FullName, "*.*").Where(x => Tools.GetHashMD5(new FileInfo(x)).Equals(media.Hash)).FirstOrDefault();
if (f != null)
{
return Path.Combine(GetStorageFolderPath(entity, propertyName), f);
}
}
}
if (entity != null)
return Path.Combine(GetStorageFolderPath(entity, propertyName), name);
else
return Path.Combine(propertyName, media?.Name);
}
internal static String GetStorageFolderPath(IPtfkForm entity, String propertyName)
{
if (entity != null)
return Path.Combine(entity.ClassName, entity.Id.ToString(), propertyName);
throw new FileNotFoundOnStorageException("Storage Path has not been generated!");
}
internal static StorageMode StorageMode
{
get
{
var p = Settings.Storage.Path;
if (Directory.Exists(p))
return Settings.StorageMode.OnContentDB;
return Settings.StorageMode.OnDBMS;
}
}
private static void MaterializeFile(
IPtfkSession session,
long ID,
byte[] obj,
IPtfkForm entity,
string propertyName,
IPtfkMedia item = null)
{
string path2 = Guid.NewGuid().ToString() + (item == null ? "" : item.Extension);
FileInfo file1 = new FileInfo(Path.Combine(PtfkFileInfo.GetDirectoryInfo(session).FullName, path2));
File.WriteAllBytes(file1.FullName, obj);
IPtfkSession session1 = session;
PtfkFileInfo file2 = new PtfkFileInfo();
file2.FileInfo = file1;
file2.OwnerID = session.Login;
file2.UID = ID.ToString();
file2.EntityName = entity.GetType().Name;
file2.ParentID = ID;
file2.Name = file1.Name;
file2.Hash = item == null ? Tools.GetHashMD5(file1) : item.Hash;
file2.MediaID = item == null ? 0 : item.Id;
file2.EntityProperty = propertyName;
ILogger logger = entity.Logger;
PtfkFileInfo.AddFile(session1, file2, logger);
}
private static Boolean IsNewFile(PtfkFileInfo file)
{
long lID = -1;
long.TryParse(file.UID, out lID);
if (lID < 0)
return true;
return false;
}
internal static long SaveMedia(IPtfkForm entity, IPtfkMedia media, IPtfkSession session)
{
try
{
object instance = Activator.CreateInstance(PtfkFileInfo.BusinessClassMedia.GetType(), session);
object toClass = Activator.CreateInstance(PtfkFileInfo.BusinessClassMedia.GetType().BaseType.GetGenericArguments()[0]);
foreach (var prop in media.GetType().GetProperties())
{
object val = prop.GetValue(media);
if (val != null && prop.SetMethod != (MethodInfo)null)
toClass.GetType().GetProperty(prop.Name).SetValue(toClass, val);
}
IPtfkMedia ptfkMedia = (((IEnumerable<MethodInfo>)PtfkFileInfo.BusinessClassMedia.GetType().GetMethods()).Where<MethodInfo>((Func<MethodInfo, bool>)(method => method.Name == nameof(IPtfkBusiness<object>.List) && ((IEnumerable<ParameterInfo>)method.GetParameters()).Count<ParameterInfo>() == 0)).FirstOrDefault<MethodInfo>().Invoke(instance, (object[])null) as IQueryable<IPtfkMedia>).Where<IPtfkMedia>((System.Linq.Expressions.Expression<Func<IPtfkMedia, bool>>)(m => m.Hash.Equals(media.Hash) && m.EntityId.Equals(media.EntityId) && m.EntityName.Equals(media.EntityName) && m.EntityProperty.Equals(media.EntityProperty))).FirstOrDefault<IPtfkMedia>();
if (ptfkMedia != null && ptfkMedia.Id > 0)
{
switch (StorageMode)
{
case StorageMode.OnContentDB:
var file = new FileInfo(GetStorageAbsoluteFilePath(entity, media.EntityProperty, media, true));
if (!file.Directory.Exists)
file.Directory.Create();
if (media.Bytes != null && !file.FullName.EndsWith(ptfkMedia.Path))
{
File.WriteAllBytes(file.FullName, media.Bytes);
ptfkMedia.Bytes = null;
ptfkMedia.Path = file.FullName.Substring(file.FullName.IndexOf(GetStorageFolderPath(entity, media.EntityProperty)));
}
break;
default:
break;
}
ptfkMedia.IsDeath = null;
toClass = ptfkMedia;
}
else
{
{
switch (StorageMode)
{
case StorageMode.OnContentDB:
IPtfkMedia iMedia = toClass as IPtfkMedia;
var file = new FileInfo(GetStorageAbsoluteFilePath(entity, iMedia.EntityProperty, iMedia, true));
if (!file.Directory.Exists)
file.Directory.Create();
if (iMedia.Bytes != null)
{
File.WriteAllBytes(file.FullName, iMedia.Bytes);
iMedia.Bytes = null;
iMedia.Path = file.FullName.Substring(file.FullName.IndexOf(GetStorageFolderPath(entity, iMedia.EntityProperty)));
}
break;
default:
break;
}
}
}
(PtfkFileInfo.BusinessClassMedia.GetType().GetMethod(nameof(IPtfkBusiness<object>.Save)).Invoke(instance, new object[1] { toClass }) as Task).Wait();
return (toClass as IPtfkEntity).Id;
}
catch (Exception ex)
{
return 0;
}
}
internal static void RefreshFile(IPtfkSession session, FileInfo file, FileInfo newLocation)
{
foreach (PtfkFileInfo ptfkFileInfo in ((IEnumerable<PtfkFileInfo>)PtfkFileInfo.Get(session)).Where<PtfkFileInfo>((Func<PtfkFileInfo, bool>)(x => x.FileInfo.FullName.ToLower().Equals(file.FullName.ToLower()))))
ptfkFileInfo.FileInfo = new FileInfo(newLocation.FullName);
}
internal static async void InactivateMedias(IPtfkSession session, long entityID, string entityName, params long[] ActiveIDs)
{
try
{
//Not inactivate if ActiveIDs is empty
if (ActiveIDs.Count() == 0)
return;
var instance = Activator.CreateInstance(BusinessClassMedia.GetType(), new object[] { session });
var method = BusinessClassMedia.GetType().GetMethods().Where(x => x.Name.Equals(nameof(IPtfkBusiness<object>.List)) && x.GetParameters().Length == 0).FirstOrDefault();
var list = method.Invoke(instance, null) as IQueryable<IPtfkMedia>;
var all = list.Where(x => x != null && !String.IsNullOrEmpty(x.EntityName) && x.EntityId.Equals(entityID) && x.EntityName.Equals(entityName)).ToList();
var medias = (from m in all
where m.EntityId.Equals(entityID) && m.EntityName.Equals(entityName) &&
string.IsNullOrWhiteSpace(m.IsDeath) &&
!ActiveIDs.Contains(m.Id)
select m).ToList();
var type = BusinessClassMedia.GetType().BaseType.GetGenericArguments()[0];
foreach (var item in medias)
{
item.IsDeath = "1";
await (BusinessClassMedia.GetType().GetMethod(nameof(IPtfkBusiness<object>.Update)).Invoke(instance, new object[] { item }) as System.Threading.Tasks.Task);
}
}
catch (Exception ex)
{
}
}
}
}