-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileMetaInfo.cs
126 lines (103 loc) · 3.39 KB
/
FileMetaInfo.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
using System;
using System.Drawing;
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System.Text.RegularExpressions;
using ExifTags = SixLabors.ImageSharp.MetaData.Profiles.Exif;
namespace PicSort
{
internal class FileMetaInfo
{
public DateTime DT;
public string DirName;
//private string fileName;
private void InitMembers(DateTime dt)
{
this.DT = dt;
//this.DirName = $"{dt.Year:D4}-{dt.Month:D2}-{dt.Day:D2}_{dt.Hour:D2}-{dt.Minute:D2}-{dt.Se:D2}";
this.DirName = $"{dt.Year:D4}-{dt.Month:D2}";
}
public FileMetaInfo(DateTime dt)
{
InitMembers(dt);
}
public FileMetaInfo(string fileName)
{
var dt = Filename2Datetime.MakeDatetime(fileName);
if (dt != null)
{
InitMembers(dt.Value);
}
else
{
Console.WriteLine($"Could not parse {fileName}");
InitMembers(DateTime.MinValue);
}
}
internal static FileMetaInfo MetaInfoFactory(string fullName, string fileName, string fileExt)
{
FileMetaInfo mi = null;
switch (fileExt.ToLower())
{
case ".jpg" :
mi = ReadJpegMetaInfo(fullName);
if (mi == null)
{
mi = new FileMetaInfo(fileName);
}
break;
case ".mov" :
mi = new FileMetaInfo(new FileInfo(fullName).LastWriteTime);
break;
case ".mp4" :
mi = new FileMetaInfo(fileName);
break;
case ".mpg" :
mi = new FileMetaInfo(new FileInfo(fullName).LastWriteTime);
break;
case ".avi" :
mi = new FileMetaInfo(new FileInfo(fullName).LastWriteTime);
break;
case ".mts" :
mi = new FileMetaInfo(new FileInfo(fullName).LastWriteTime);
break;
case ".mkv" :
mi = new FileMetaInfo(new FileInfo(fullName).LastWriteTime);
break;
/*
.3gp
.mkv
*.mpg
.mts
.gif
*/
}
return mi;
}
private static FileMetaInfo ReadJpegMetaInfo(string fullName)
{
ExifTags.ExifValue dtValue;
using (var image = Image.Load(fullName))
{
if (image.MetaData.ExifProfile == null)
{
return null;
}
var exif = image.MetaData.ExifProfile;
if (!exif.TryGetValue(ExifTags.ExifTag.DateTime, out dtValue))
{
return null;
}
}
var dtS = (String)dtValue.Value;
var dt = Filename2Datetime.MakeDatetime(dtS, Filename2Datetime.RegexType.Exif);
if (dt != null)
{
return new FileMetaInfo(dt.Value);
}
Console.WriteLine($"{fullName} has EXIF, however could not parse {dtS}");
return null;
}
}
}