-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
249 lines (190 loc) · 6.77 KB
/
MainWindow.xaml.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
using ExifLib;
using MediaInfoDotNet;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RPictureArrange
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string targetPath;
public MainWindow()
{
InitializeComponent();
RegistryKey pRegKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\LimInfi", true);
if (pRegKey == null)
{
pRegKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\LimInfi");
}
targetPath = pRegKey.GetValue("TargetPath") as string;
if (targetPath == null)
{
pRegKey.SetValue("TargetPath", @"C:\PHOTOS\");
targetPath = @"C:\PHOTOS\";
}
targetPath = @"C:\PHOTOS\";
this.AllowDrop = true;
targetDirTextBox.Text = targetPath;
}
private void selectPath_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
targetDirTextBox.Text = openFileDialog.FileName;
}
}
private void Grid_DragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Move;
}
MD5 md5 = new MD5CryptoServiceProvider();
private void ProcessOnFile(string sf, string extName)
{
// #1 Get MD5 Filename
byte[] contents = File.ReadAllBytes(sf);
byte[] output = md5.ComputeHash(contents);
string newFileName = BitConverter.ToString(output).Replace("-", "") + extName;
// #2 Get Taken DateTime
string outDirName = "UNKNOWN";
try
{
using (ExifReader reader = new ExifReader(sf))
{
DateTime datePictureTaken;
if (reader.GetTagValue(ExifTags.DateTime, out datePictureTaken))
{
outDirName = string.Format("{0}-{1}", datePictureTaken.Year.ToString("D4"), datePictureTaken.Month.ToString("D2"));
}
}
}
catch(Exception)
{
}
// #3 Create directory if not exists
Directory.CreateDirectory(this.targetPath + outDirName);
File.WriteAllBytes(this.targetPath + outDirName + @"\" + newFileName + extName, contents);
File.Delete(sf);
}
private void ProcessMovieFile(string sf, string extName)
{
// #1 Get MD5 Filename
byte[] output = null;
using (FileStream fs = new FileStream(sf, FileMode.Open))
{
output = md5.ComputeHash(fs);
}
string newFileName = BitConverter.ToString(output).Replace("-", "") + extName;
// #2 Get Taken DateTime
string outDirName = "UNKNOWN";
try
{
MediaFile mdd = new MediaFile(sf);
DateTime datePictureTaken = mdd.Video[0].EncodedDate;
outDirName = string.Format("Movie\\{0}-{1}", datePictureTaken.Year.ToString("D4"), datePictureTaken.Month.ToString("D2"));
}
catch (Exception)
{
}
// #3 Create directory if not exists
Directory.CreateDirectory(this.targetPath + outDirName);
string nf = "";
if (extName == ".mts" || extName == ".avi" || extName == ".mpg")
{
nf = System.IO.Path.GetFileName(sf);
}
else nf = newFileName + extName;
// Move File
if (!File.Exists(this.targetPath + outDirName + @"\" + nf))
{
File.Move(sf, this.targetPath + outDirName + @"\" + nf);
}
else
{
File.Delete(sf);
}
}
private void GetAllDirList(string strBaseDir)
{
DirectoryInfo di = new DirectoryInfo(strBaseDir);
// #1.1 Get All Files
FileInfo[] fis = di.GetFiles("*.jpg");
foreach(FileInfo fi in fis)
{
ProcessOnFile(fi.FullName, ".jpg");
}
// #1.2 Get All MTS File
fis = di.GetFiles("*.mts");
foreach (FileInfo fi in fis)
{
ProcessMovieFile(fi.FullName, ".mts");
}
// #1.3 Get All MOV File
fis = di.GetFiles("*.mov");
foreach (FileInfo fi in fis)
{
ProcessMovieFile(fi.FullName, ".mov");
}
// #1.4 Get All MOV File
fis = di.GetFiles("*.mp4");
foreach (FileInfo fi in fis)
{
ProcessMovieFile(fi.FullName, ".mp4");
}
// #1.5 Get All MOV File
fis = di.GetFiles("*.avi");
foreach (FileInfo fi in fis)
{
ProcessMovieFile(fi.FullName, ".avi");
}
// #1.6 Get All MOV File
fis = di.GetFiles("*.mpg");
foreach (FileInfo fi in fis)
{
ProcessMovieFile(fi.FullName, ".mpg");
}
// #2 Recursive to directory
foreach (DirectoryInfo subdi in di.GetDirectories())
{
GetAllDirList(subdi.FullName);
}
}
private void Grid_Drop(object sender, DragEventArgs e)
{
string[] droppedFilenames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach(string sf in droppedFilenames)
{
FileAttributes attr = File.GetAttributes(sf);
//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
GetAllDirList(sf);
}
else
{
string ext = System.IO.Path.GetExtension(sf).ToLower();
//if (ext == ".jpg" || ext == ".png") ProcessOnFile(sf, extName);
if (ext == ".jpg") ProcessOnFile(sf, ext);
}
}
}
}
}