Skip to content

Commit b091cdd

Browse files
author
Simon Langbehn
committed
Store all repositories if storage is empty
1 parent 1c9b02b commit b091cdd

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

Control/AzureStorage.cs

+37-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
using System;
44
using System.IO;
55
using DataOne.BitUp.Properties;
6-
6+
using System.Linq;
7+
78
namespace DataOne.BitUp
89
{
910
public class AzureStorage : IStorage
@@ -12,23 +13,13 @@ public void SaveFile(string filePath)
1213
{
1314
string fileName = Path.GetFileName(filePath);
1415

15-
string accountName = Settings.Default.AzureAccountName;
16-
string accountKey = Settings.Default.AzureAccountKey;
1716
string storageContainer = Settings.Default.AzureStorageContainer;
18-
string defaultEndpointsProtocol = Resources.AzureEndpointsProtocol;
17+
string accountName = Settings.Default.AzureAccountName;
1918
string fileLocation = Resources.AzureFileLocation;
2019
string fileUri = Resources.AzureFileUri;
2120

22-
string connectionKey = string.Concat(defaultEndpointsProtocol, "AccountName=", accountName, ";AccountKey=", accountKey, ";");
23-
24-
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionKey);
25-
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
26-
27-
CloudFileShare share = fileClient.GetShareReference(storageContainer);
28-
share.CreateIfNotExists();
29-
30-
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
31-
rootDir.CreateIfNotExists();
21+
var fileClient = GetFileClient();
22+
var rootDir = EnsureRootDirectory(fileClient);
3223

3324
var cloudFileUrl = string.Concat(fileLocation, accountName, fileUri, storageContainer, "/", fileName);
3425
var uriToFile = new Uri(cloudFileUrl);
@@ -42,5 +33,37 @@ public string GetName()
4233
return "Azure File Storage";
4334
}
4435

36+
public bool IsEmpty()
37+
{
38+
var fileClient = GetFileClient();
39+
var rootDir = EnsureRootDirectory(fileClient);
40+
var content = rootDir.ListFilesAndDirectories().ToList();
41+
return content.Count > 0;
42+
}
43+
44+
private CloudFileClient GetFileClient()
45+
{
46+
string defaultEndpointsProtocol = Resources.AzureEndpointsProtocol;
47+
string accountName = Settings.Default.AzureAccountName;
48+
string accountKey = Settings.Default.AzureAccountKey;
49+
50+
string connectionKey = string.Concat(defaultEndpointsProtocol, "AccountName=", accountName, ";AccountKey=", accountKey, ";");
51+
52+
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionKey);
53+
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
54+
55+
return fileClient;
56+
}
57+
58+
private CloudFileDirectory EnsureRootDirectory(CloudFileClient fileClient)
59+
{
60+
string storageContainer = Settings.Default.AzureStorageContainer;
61+
CloudFileShare share = fileClient.GetShareReference(storageContainer);
62+
share.CreateIfNotExists();
63+
64+
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
65+
rootDir.CreateIfNotExists();
66+
return rootDir;
67+
}
4568
}
4669
}

Control/LocalStorage.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@ namespace DataOne.BitUp
44
{
55
public class LocalStorage : IStorage
66
{
7+
private const string RepositoryFolderName = @".\DataOne.BitUp.RepositoryBackups";
8+
79
public string GetName()
810
{
911
return "local file system";
10-
}
11-
12+
}
13+
1214
public void SaveFile(string filePath)
1315
{
1416
try
1517
{
16-
Directory.CreateDirectory(@".\DataOne.BitUp.RepositoryBackups");
18+
Directory.CreateDirectory(RepositoryFolderName);
1719
}
1820
finally
1921
{
20-
File.Copy(filePath, @".\DataOne.BitUp.RepositoryBackups\" + Path.GetFileName(filePath), true);
22+
File.Copy(filePath, RepositoryFolderName + @"\" + Path.GetFileName(filePath), true);
2123
}
2224
}
25+
26+
public bool IsEmpty()
27+
{
28+
var subDirs = Directory.GetDirectories(RepositoryFolderName);
29+
var files = Directory.GetFiles(RepositoryFolderName);
30+
return subDirs.Length > 0 && files.Length > 0;
31+
}
2332
}
2433
}

Interfaces/IStorage.cs

+6
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,11 @@ interface IStorage
1414
/// </summary>
1515
/// <returns>The name of the location where files are saved, e.g. Azure, local file system...</returns>
1616
string GetName();
17+
18+
/// <summary>
19+
/// Get the information whether the storage contents any files or directories
20+
/// </summary>
21+
/// <returns>Boolean value whether the storage is empty</returns>
22+
bool IsEmpty();
1723
}
1824
}

Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void Main()
3030
}
3131
var bitbucketTeams = Settings.Default.BitbucketTeams;
3232
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
33-
DateTime lastBackup = DateTime.Now.AddDays(-1.0);
33+
DateTime lastBackup = storage.IsEmpty() ? DateTime.MinValue : DateTime.Now.AddDays(-1.0);
3434

3535
string workingDirectory = string.Concat(Directory.GetCurrentDirectory(), @"\DataOne.BitUp.TempRepositoryBackups");
3636
ForceDeleteDirectory(workingDirectory);

0 commit comments

Comments
 (0)