-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
185 lines (168 loc) · 8.05 KB
/
Program.cs
File metadata and controls
185 lines (168 loc) · 8.05 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
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace VSCodeRecoveryTool
{
internal class Program
{
static void Main(string[] args)
{
Restore restore = new Restore();
restore.Run();
}
}
public class Entry
{
public String? id { get; set; }
public Int64? timestamp { get; set; }
}
public class JSONFileRoot
{
public Int32? version { get; set; }
public String? resource { get; set; }
public List<Entry>? entries { get; set; }
}
public class Restore
{
public void cw(String? text)
{
Console.WriteLine(text);
}
public void Run()
{
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfiguration configuration = configurationBuilder.Build();
String? VSCodeHistoryDirectory = configuration["AppSettings:VSCodeHistoryDirectory"];
String? VSCodeRecoveryDirectory = configuration["AppSettings:VSCodeRecoveryDirectory"];
if (VSCodeRecoveryDirectory != null)
{
try
{
Int32 restoredFileCount = 0;
if (Directory.Exists(VSCodeRecoveryDirectory))
{
Directory.Delete(VSCodeRecoveryDirectory, true);
}
Directory.CreateDirectory(VSCodeRecoveryDirectory);
if (VSCodeHistoryDirectory != null)
{
var vsCodeHistoryDirectoryInfo = new DirectoryInfo(VSCodeHistoryDirectory);
var vsCodeHistoryDirectories = vsCodeHistoryDirectoryInfo.EnumerateDirectories()
.OrderBy(d => d.LastWriteTime)
.Select(d => d.Name)
.ToList();
foreach (var snapshotDirectory in vsCodeHistoryDirectories)
{
String snapshotDirectoryFullPath = VSCodeHistoryDirectory + "/" + snapshotDirectory;
var snapshotDirectoryInfo = new DirectoryInfo(snapshotDirectoryFullPath);
var snapshotFiles = snapshotDirectoryInfo.EnumerateFiles()
.OrderBy(f => f.LastWriteTime)
.Select(f => f.Name)
.ToList();
Int32 fileCount = 0;
JSONFileRoot? entriesJSONFile;
foreach (var snapshotFile in snapshotFiles)
{
fileCount+=1;
var snapshotFileInfo = new FileInfo(snapshotFile);
if (fileCount == 1)
{
//export the latest file
var jsonFile = snapshotDirectoryFullPath + "/entries.json";
String? originalFilePath = "";
if (File.Exists(jsonFile))
{
using (StreamReader reader = new StreamReader(jsonFile))
{
String jsonFileString = reader.ReadToEnd();
if (jsonFileString != null)
{
entriesJSONFile = JsonConvert.DeserializeObject<JSONFileRoot>(jsonFileString);
if (entriesJSONFile != null && entriesJSONFile.entries != null)
{
List<Entry>? entries = entriesJSONFile.entries;
String? resource = entriesJSONFile.resource;
if (resource != null) { originalFilePath = resource.Replace("file://",""); }
String? fileId = "";
String? fileTimestamp = "";
String? snapshotFileFullPath = "";
String? restoreToFilePath = "";
String? dirCheckString = "";
String? originalFileName = "";
String? restoreToFullPath = "";
if (entries != null)
{
Int32 i = 0;
List<Entry>? entriesOrderedByDateDescList = entries.OrderByDescending(x => x.timestamp).ToList();
foreach (Entry? orderedEntry in entriesOrderedByDateDescList)
{
restoreToFilePath = "";
snapshotFileFullPath = "";
if (orderedEntry != null && orderedEntry.timestamp != null)
{
if (i == 0) //get first in descending by date list to restore latest file
{
restoreToFilePath = VSCodeRecoveryDirectory + originalFilePath;
fileId = orderedEntry.id;
fileTimestamp = orderedEntry.timestamp.ToString();
originalFileName = "";
restoreToFullPath = "";
//restore original file to full path
snapshotFileFullPath = snapshotDirectoryFullPath + "/" + fileId;
String[] restorePathArray = restoreToFilePath.Split('/');
originalFileName = restorePathArray[restorePathArray.Length - 1];
Int32 j = 0;
foreach (var dir in restorePathArray)
{
if (dir != "")
{
if (j == restorePathArray.Length - 1) //end of path array or filename
{
restoreToFullPath = dirCheckString + "/" + dir;
if (File.Exists(restoreToFullPath))
{
File.Delete(restoreToFullPath);
}
File.Copy(snapshotFileFullPath, restoreToFullPath); restoredFileCount += 1;
}
else
{
dirCheckString += "/" + dir;
if (!Directory.Exists(dirCheckString))
{
Directory.CreateDirectory(dirCheckString);
}
}
}
j+=1;
}
}
}
i+=1;
}
}
}
}
}
}
}
}
}
}
cw(restoredFileCount.ToString() + " files restored to \n"+VSCodeRecoveryDirectory);
}
catch (IOException ioex)
{
cw("IO Exception : " + ioex.Message);
}
catch (Exception ex)
{
cw("Exception : " + ex.Message);
}
}
}
}
}