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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
namespace SourceBrowser.Site.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using SourceBrowser.Generator;
    using SourceBrowser.Site.Models;
    using SourceBrowser.Site.Utilities;
    using SourceBrowser.SolutionRetriever;

    internal static class BrowserRepository
    {
        private static readonly string StaticHtmlAbsolutePath = System.Web.Hosting.HostingEnvironment.MapPath("~/SB_Files/");
        /// <summary>
        /// Lock used for querying repo's status (n/a, processing or ready)
        /// </summary>
        private static object fileOperationLock = new object();

        // TODO: DO NOT LOAD THIS INTO MEMORY.
        // This string could be really big. We should probably be distributing this
        // from a CDN. However, we're not saving them to a CDN, so we'll have to
        // figure that out first.
        internal static string GetDocumentHtml(string username, string repository, string path)
        {
            var fullPath = Path.Combine(StaticHtmlAbsolutePath, username, repository, path);

            using(var sr = new StreamReader(fullPath))
            {
                var rawHtml = sr.ReadToEnd();
                return rawHtml;
            }
        }

        internal static bool TryLockRepository(string userName, string repoName)
        {
	        string lockFileDirectory = Path.Combine(StaticHtmlAbsolutePath, userName, repoName);
            if (Directory.Exists(lockFileDirectory))
            {
                // The directory already exists, we can't modify this repository.
                return false;
            }
            string lockFilePath = Path.Combine(lockFileDirectory, Constants.REPO_LOCK_FILENAME);
            lock (fileOperationLock)
            {
                if (File.Exists(lockFilePath))
                {
                    // Looks like someone is already working with this repository.
                    return false;
                }
                else
                {
                    // Create a file that indicates that the upload will begin
                    Directory.CreateDirectory(lockFileDirectory);
                    // Properly dispose of the filesystem lock
                    using (var stream = File.Create(lockFilePath))
                    {
                        return true;
                    }
                }
            }
        }

        internal static void UnlockRepository(string userName, string repoName)
        {
	        string lockFilePath = Path.Combine(StaticHtmlAbsolutePath, userName, repoName, Constants.REPO_LOCK_FILENAME);
            lock (fileOperationLock)
            {
                if (File.Exists(lockFilePath))
                {
                    File.Delete(lockFilePath);
                }
            }
        }

        internal static bool IsRepositoryReady(string userName, string repoName)
        {
            string lockFilePath = Path.Combine(StaticHtmlAbsolutePath, userName, repoName, Constants.REPO_LOCK_FILENAME);
            lock (fileOperationLock)
            {
                if (File.Exists(lockFilePath))
                {
                    return false;
                }
            }
            return true;
        }

        internal static void RemoveRepository(string userName, string repoName)
        {
            string lockFileDirectory = Path.Combine(StaticHtmlAbsolutePath, userName, repoName);
            lock (fileOperationLock)
            {
                if (Directory.Exists(lockFileDirectory))
                {
                    Directory.Delete(lockFileDirectory, true);
                }
            }
        }

        internal static bool PathExists(string username, string repository = "", string path = "")
        {
            var fullPath = Path.Combine(StaticHtmlAbsolutePath, username, repository, path);
            return Directory.Exists(fullPath);
        }

        internal static bool FileExists(string username, string repository, string path)
        {
            var fullPath = Path.Combine(StaticHtmlAbsolutePath, username, repository, path);
            return File.Exists(fullPath);
        }

        /// <summary>
        /// Returns a list of all Github users on file.
        /// </summary>
        /// <returns>
        /// All Github users.
        /// </returns>
        public static List<GithubUserStructure> GetAllGithubUsers()
        {
            // If, for some reason, we have no data at all, just return an empty list
            if (!Directory.Exists(StaticHtmlAbsolutePath))
            {
                return new List<GithubUserStructure>();
            }

            // Otherwise, find them all
            var directories = Directory.GetDirectories(StaticHtmlAbsolutePath);
            var users = new List<GithubUserStructure>(directories.Length);
            foreach (var directoryName in directories)
            {
                var userName = Path.GetFileName(directoryName);
                users.Add(GetUserStructure(userName));
            }
            return users;
        }

        /// <summary>
        /// Returns a structure containing information on user's github repositories available at Source Browser.
        /// If the structure does not exist, creates it.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        internal static GithubUserStructure GetUserStructure(string userName)
        {
            var userDataFile = Path.Combine(StaticHtmlAbsolutePath, userName, "user.data");
            if (!File.Exists(userDataFile))
            {
                var userData = SetUpUserStructure(userName);
                FileUtilities.SerializeData(userData, userDataFile);
                return userData;
            }
            try
            {
                return FileUtilities.DeserializeData<GithubUserStructure>(userDataFile);
            }
            catch
            {
                // There was some problem. Recreate the data.
                var userData = SetUpUserStructure(userName);
                FileUtilities.SerializeData(userData, userDataFile);
                return userData;
            }
            finally
            {
                // if user.data file is stale, asynchronously update it.
                if (!FileUtilities.FileIsFresh(userDataFile))
                {
                    Task.Run(() =>
                    {
                        var updatedData = SetUpUserStructure(userName);
                        FileUtilities.SerializeData(updatedData, userDataFile);
                    });
                }
            }
        }
    
        /// <summary>
        /// Creates a structure containing information on user's github repositories available at Source Browser.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns>The github structure.</returns>
        internal static GithubUserStructure SetUpUserStructure(string userName)
        {
            var repoPath = Path.Combine(StaticHtmlAbsolutePath, userName);

            List<GithubRepoStructure> repos;
            if (Directory.Exists(repoPath))
            {
                var directories = Directory.GetDirectories(repoPath);
                repos = new List<GithubRepoStructure>(directories.Length);
                foreach (var directory in directories)
                {
                    var repoName = Path.GetFileName(directory);
                    repos.Add(GetRepoStructure(userName, repoName));
                }
            }
            else
            {
                // If, for some reason the directory doesn't exist, just supply an empty list
                repos = new List<GithubRepoStructure>();
            }

            var userData = new GithubUserStructure()
            {
                Username = userName,
                Repos = repos,
                Path = repoPath
            };
            userData.UseLiveData();

            return userData;
        }

        /// <summary>
        /// Returns a structure containing information on a repository available at Source Browser.
        /// If the structure does not exist, creates it.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="repoName"></param>
        /// <returns></returns>
        internal static GithubRepoStructure GetRepoStructure(string userName, string repoName)
        {
            var repoDataFile = Path.Combine(StaticHtmlAbsolutePath, userName, repoName, "repo.data");
            if (!File.Exists(repoDataFile))
            {
                var repoData = SetUpRepoStructure(userName, repoName);
                FileUtilities.SerializeData(repoData, repoDataFile);
                return repoData;
            }
            try
            {
                return FileUtilities.DeserializeData<GithubRepoStructure>(repoDataFile);
            }
            catch
            {
                // There was some problem. Recreate the data.
                var repoData = SetUpRepoStructure(userName, repoName);
                FileUtilities.SerializeData(repoData, repoDataFile);
                return repoData;
            }
            finally
            {
                // if repo.data file is stale, asynchronously update it.
                if (!FileUtilities.FileIsFresh(repoDataFile))
                {
                    Task.Run(() =>
                    {
                        var updatedData = SetUpRepoStructure(userName, repoName);
                        FileUtilities.SerializeData(updatedData, repoDataFile);
                    });
                }
            }
        }

        /// <summary>
        /// Creates a structure containing information on a repository available at Source Browser.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="repoName"></param>
        /// <returns></returns>
        internal static GithubRepoStructure SetUpRepoStructure(string userName, string repoName)
        {
            // Currently unused, might be useful at some point
            // var repoRoot = Path.Combine(StaticHtmlAbsolutePath, userName, repoName);

            var repoData = new GithubRepoStructure()
            {
                Name = repoName,
                ParentUserName = userName
            };
            repoData.UseLiveData();

            return repoData;
        }

        internal static GithubSolutionStructure SetUpSolutionStructure(string userName, string repoName, string solutionName)
        {
    var viewModel = new GithubSolutionStructure()
            {
                Name = solutionName,
                RelativePath = CreatePath(userName, repoName, solutionName),
                RelativeRootPath = CreatePath(userName, repoName, solutionName),
                ParentRepo = SetUpRepoStructure(userName, repoName)
            };

            return viewModel;
        }

        internal static GithubFileStructure SetUpFileStructure(string userName, string repoName, string path, string html) 
        {
            var viewModel = new GithubFileStructure
            {
                FileName = Path.GetFileName(path),
                Directory = GetRelativeDirectory(path),
                RelativePath = CreatePath(userName, repoName, GetRelativeDirectory(path)), // Used to expand nodes leading to this file
                RelativeRootPath = CreatePath(userName, repoName, path), // Points to the root of the treeview
                SourceCode = html,
            };

            return viewModel;
        }

        public static string GetRelativeDirectory(string path)
        {
            var fileName = Path.GetFileName(path);
            if (fileName != null)
            {
                var baseDirectory = path.Substring(0, path.Length - fileName.Length);
                return baseDirectory;
            }
            return null;
        }

        public static List<string> FindFiles(string path)
        {
            var fullPath = Path.Combine(StaticHtmlAbsolutePath, path);
            var files = new List<string>();
            if (!Directory.Exists(fullPath))
            {
                return files;
            }

            var filePaths = Directory.GetFiles(fullPath);
            files = new List<string>(filePaths);
            files = files.Where(n => n != "solutionInfo.json").ToList();
            return files;
        }

        internal static List<string> FindFolders(string path)
        {
            var fullPath = Path.Combine(StaticHtmlAbsolutePath, path);
            var directories = new List<string>();
            if (!Directory.Exists(fullPath))
            {
                return directories;
            }

            var directoryPaths = Directory.GetDirectories(fullPath);
            directories = new List<string>(directoryPaths);
            return directories;
        }

        private static string CreatePath(string part1, string part2 = null, string part3 = null, string part4 = null)
        {
            string[] pathParts;
            if (part4 != null)
            {
                pathParts = new[] { part1, part2, part3, part4 };
            }
            else if (part3 != null)
            {
                pathParts = new[] { part1, part2, part3 };
            }
            else if (part2 != null)
            {
                pathParts = new[] { part1, part2 };
            }
            else
            {
                pathParts = new[] { part1 };
            }

            return string.Join("/", pathParts);
        }
    }
}