forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecoveryManager.cs
More file actions
74 lines (69 loc) · 2.36 KB
/
Copy pathRecoveryManager.cs
File metadata and controls
74 lines (69 loc) · 2.36 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
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using FlashDevelop.Helpers;
using PluginCore.Utilities;
using PluginCore.Managers;
using PluginCore.Helpers;
namespace FlashDevelop.Managers
{
class RecoveryManager
{
/// <summary>
/// Removes the saved backup file from the restoration directory
/// </summary>
public static void RemoveTemporaryFile(String file)
{
try
{
String name = ConvertToFileName(file) + ".bak";
String path = Path.Combine(FileNameHelper.RecoveryDir, name);
if (File.Exists(path)) File.Delete(path);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Saves a backup file to the recovery directory
/// </summary>
public static void SaveTemporaryFile(String file, String text, Encoding encoding)
{
try
{
String name = ConvertToFileName(file) + ".bak";
String recoveryDir = FileNameHelper.RecoveryDir;
if (!Directory.Exists(recoveryDir)) Directory.CreateDirectory(recoveryDir);
String path = Path.Combine(recoveryDir, name); // Create full file path
Int32 lineEndMode = LineEndDetector.DetectNewLineMarker(text, (Int32)Globals.Settings.EOLMode);
String lineEndMarker = LineEndDetector.GetNewLineMarker(lineEndMode);
text = file + lineEndMarker + lineEndMarker + text;
FileHelper.WriteFile(path, text, encoding);
}
catch (Exception ex)
{
ErrorManager.ShowError(ex);
}
}
/// <summary>
/// Converts a path to a valid file name
/// </summary>
private static String ConvertToFileName(String path)
{
try
{
return HashCalculator.CalculateSHA1(path);
}
catch
{
String filename = Path.GetFullPath(path);
String separator = Path.DirectorySeparatorChar.ToString();
filename = path.Replace(separator, ".");
filename = path.Replace(" ", "_");
return filename;
}
}
}
}