-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSaveFileHandler.cs
More file actions
330 lines (312 loc) · 13.3 KB
/
Copy pathSaveFileHandler.cs
File metadata and controls
330 lines (312 loc) · 13.3 KB
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
/*
* Media Extractor is an application to preview and extract packed media in Microsoft Office files (e.g. Word, PowerPoint or Excel documents)
* Copyright Raphael Stoeckli © 2025
* This program is licensed under the MIT License.
* You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
*/
using AdonisUI.Controls;
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace MediaExtractor
{
/// <summary>
/// Class to handle saving embedded files
/// </summary>
public class SaveFileHandler
{
/// <summary>
/// Enum to define the default method for saving multiple files
/// </summary>
public enum DefaultSaveMethod
{
/// <summary>
/// All files are saved
/// </summary>
All,
/// <summary>
/// Only the selected files are saves
/// </summary>
Selected
}
/// <summary>
/// Current view model
/// </summary>
private ViewModel CurrentModel { get; set; }
/// <summary>
/// Default save method
/// </summary>
public DefaultSaveMethod DefaultMethod { get; set; } = DefaultSaveMethod.All;
/// <summary>
/// Constructor with parameter
/// </summary>
/// <param name="currentModel">View model to apply</param>
public SaveFileHandler(ViewModel currentModel)
{
this.CurrentModel = currentModel;
this.CurrentModel.CurrentSaveFileHandler = this;
}
/// <summary>
/// Method to perform the default save action
/// </summary>
public void SaveDefault()
{
if (!CurrentModel.SaveAllIsDefault && !CurrentModel.SaveSelectedIsDefault)
{
if (CurrentModel.SelectedItems.Length > 0)
{
SaveSelectedFiles();
}
else
{
SaveAllFiles();
}
}
else
{
if (DefaultMethod == DefaultSaveMethod.All)
{
SaveAllFiles();
}
else
{
SaveSelectedFiles();
}
}
}
/// <summary>
/// Method to save all files
/// </summary>
public void SaveAllFiles()
{
ListViewItem[] items = CurrentModel.ListViewItems.ToArray();
SaveFileRange(items, I18n.T(I18n.Key.DialogSaveAllTitle));
}
/// <summary>
/// Method to save the selected files
/// </summary>
public void SaveSelectedFiles()
{
if (CurrentModel.SelectedItems.Length == 1)
{
SaveSingleFile(CurrentModel.SelectedItems.First());
}
else
{
SaveFileRange(CurrentModel.SelectedItems, I18n.T(I18n.Key.DialogSaveSelectedTitle));
}
}
/// <summary>
/// Method to save the currently selected entry as file
/// </summary>
private void SaveSingleFile(ListViewItem item)
{
try
{
SaveFileDialog sfd = new SaveFileDialog
{
Title = I18n.T(I18n.Key.DialogSaveCurrentTitle),
Filter = I18n.T(I18n.Key.DialogSaveFilter), // All files|*.*
FileName = item.FileName
};
bool? result = sfd.ShowDialog();
if (result == true)
{
Save(item.FileReference, sfd.FileName, true);
if (CurrentModel.ShowInExplorer)
{
FileInfo fi = new FileInfo(sfd.FileName);
bool open = Utils.ShowInExplorer(fi.DirectoryName);
if (!open)
{
MessageBox.Show(I18n.R(I18n.Key.TextSaveError, fi.DirectoryName), I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
}
}
catch
{
// ignore
}
}
/// <summary>
/// Method to save all files
/// </summary>
private void SaveFileRange(ListViewItem[] items, string dialogMessage)
{
try
{
Ookii.Dialogs.Wpf.VistaFolderBrowserDialog ofd = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog
{
Description = dialogMessage,
UseDescriptionForTitle = true
};
bool? res = ofd.ShowDialog();
if (res.HasValue && res.Value)
{
bool fileExists, check;
int errors = 0;
int skipped = 0;
int extracted = 0;
int renamed = 0;
int overwritten = 0;
FileInfo fi;
ExistingFileDialog.ResetDialog();
foreach (ListViewItem item in items)
{
fileExists = CheckFileExists(ofd.SelectedPath, item.FileReference, CurrentModel.KeepFolderStructure, out var fileName);
if (fileExists && (ExistingFileDialog.RememberDecision == null || !ExistingFileDialog.RememberDecision.Value))
{
fi = new FileInfo(fileName);
uint crc = Utils.GetCrc(fileName);
ExistingFileDialog efd = new ExistingFileDialog(fi.Name, fi.LastWriteTime, fi.Length, crc, item.FileName, item.FileReference.LastChange, item.FileReference.FileSize, item.FileReference.Crc32);
efd.ShowDialog();
}
if (fileExists)
{
if (ExistingFileDialog.DialogResult == ExistingFileDialog.Result.Cancel) // Cancel extractor
{
CurrentModel.StatusText = I18n.T(I18n.Key.StatusSaveCanceled);
MessageBox.Show(I18n.T(I18n.Key.StatusSaveCanceled), I18n.T(I18n.Key.DialogCancelTitle), MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
else if (ExistingFileDialog.DialogResult == ExistingFileDialog.Result.Overwrite) // Overwrite existing
{
check = Save(item.FileReference, fileName, false);
if (!check) { errors++; }
else
{
extracted++;
overwritten++;
}
}
else if (ExistingFileDialog.DialogResult == ExistingFileDialog.Result.Rename) // Rename new
{
fileName = Utils.GetNextFileName(fileName);
check = Save(item.FileReference, fileName, false);
if (!check) { errors++; }
else
{
extracted++;
renamed++;
}
}
else if (ExistingFileDialog.DialogResult == ExistingFileDialog.Result.Skip) // Skip file
{
skipped++;
}
else
{
errors++;
}
}
else
{
check = Save(item.FileReference, fileName, false);
if (!check) { errors++; }
else { extracted++; }
}
}
if (errors > 0 || skipped > 0)
{
StringBuilder sb = new StringBuilder();
if (errors == 1) { sb.Append(I18n.T(I18n.Key.TextErrorOneFile)); }
else if (errors > 1) { sb.Append(I18n.R(I18n.Key.TextErrorMultipleFiles, errors)); }
sb.Append("\n");
if (skipped == 1) { sb.Append(I18n.T(I18n.Key.TextSkippedOneFile)); }
else if (skipped > 1) { sb.Append(I18n.R(I18n.Key.TextSkippedMultipleFiles, skipped)); }
string message;
if (sb[0] == '\n')
{
message = sb.ToString(1, sb.Length - 1);
}
else
{
message = sb.ToString();
}
CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveErrorSummary, extracted, overwritten, renamed, skipped, errors);
MessageBox.Show(message, I18n.T(I18n.Key.DialogSaveErrors), MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
else
{
CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveSummary, extracted, overwritten, renamed, skipped);
}
if (CurrentModel.ShowInExplorer)
{
bool open = Utils.ShowInExplorer(ofd.SelectedPath);
if (!open)
{
MessageBox.Show(I18n.R(I18n.Key.DialogExplorerError, ofd.SelectedPath), I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(I18n.T(I18n.Key.DialogUnexpectedError) + "\n" + ex.Message, I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// Method to save a single entry as file
/// </summary>
/// <param name="item">ExtractorItem to save</param>
/// <param name="filename">file name for the target file</param>
/// <param name="writeStatus">If true, the status of the operation will be stated</param>
/// <returns>True if the file could be saved</returns>
private bool Save(ExtractorItem item, string filename, bool writeStatus)
{
try
{
FileInfo fi = new FileInfo(filename);
if (!Directory.Exists(fi.DirectoryName))
{
Directory.CreateDirectory(fi.DirectoryName);
}
FileStream fs = new FileStream(filename, FileMode.Create);
item.Stream.Position = 0;
fs.Write(item.Stream.GetBuffer(), 0, (int)item.Stream.Length);
fs.Flush();
fs.Close();
if (writeStatus)
{
CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveSuccess, filename);
}
}
catch (Exception e)
{
if (writeStatus)
{
CurrentModel.StatusText = I18n.R(I18n.Key.StatusSaveFailure, e.Message);
MessageBox.Show(I18n.T(I18n.Key.DialogSaveFailure), I18n.T(I18n.Key.DialogErrorTitle), MessageBoxButton.OK, MessageBoxImage.Information);
return false;
}
}
return true;
}
/// <summary>
/// Method to check whether the specified input file exists
/// </summary>
/// <param name="folder">Folder</param>
/// <param name="item">Extractor Item</param>
/// <param name="fileName">Determined name and path of the existing file as output parameter</param>
/// <param name="keepFolderStructure">If true, the relative folder of the item will be added to the root folder</param>
/// <returns>True if the file exists</returns>
private bool CheckFileExists(string folder, ExtractorItem item, bool keepFolderStructure, out string fileName)
{
string separator = Path.DirectorySeparatorChar.ToString();
char[] chars = new char[] { '/', '\\' };
if (keepFolderStructure)
{
folder = folder.TrimEnd(chars) + separator + item.Path.Trim(chars);
}
else
{
folder = folder.TrimEnd(chars);
}
fileName = folder + separator + item.FileName;
return File.Exists(fileName);
}
}
}