Removed Google Drive backup storage support.
I'm satistfied with how the SFTP method has been working, and it also means the backups aren't stored by Google as a MASSIVE plus.
This commit is contained in:
parent
621f05c6dc
commit
13d51c7bf8
3 changed files with 1 additions and 171 deletions
|
@ -6,7 +6,6 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Apis.Drive.v3" Version="1.34.0.1239" />
|
||||
<PackageReference Include="SSH.NET" Version="2016.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
using Google.Apis.Auth.OAuth2.Responses;
|
||||
using Google.Apis.Json;
|
||||
using Google.Apis.Util.Store;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BackupManager
|
||||
{
|
||||
public sealed class GoogleDatastore : IDataStore
|
||||
{
|
||||
public Config Config { get; set; }
|
||||
|
||||
public GoogleDatastore(Config config)
|
||||
{
|
||||
Config = config;
|
||||
}
|
||||
|
||||
public Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
|
||||
|
||||
TokenResponse tr = new TokenResponse {
|
||||
AccessToken = Config.GoogleAccessToken,
|
||||
TokenType = Config.GoogleTokenType,
|
||||
ExpiresInSeconds = Config.GoogleTokenExpires,
|
||||
RefreshToken = Config.GoogleRefreshToken,
|
||||
IssuedUtc = Config.GoogleTokenIssued == null
|
||||
? new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
|
||||
: DateTime.Parse(Config.GoogleTokenIssued)
|
||||
};
|
||||
|
||||
tcs.SetResult((T)(object)tr); // fuck it
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public Task DeleteAsync<T>(string key)
|
||||
=> ClearAsync();
|
||||
|
||||
public Task ClearAsync()
|
||||
{
|
||||
Config.GoogleAccessToken = null;
|
||||
Config.GoogleTokenType = null;
|
||||
Config.GoogleTokenExpires = null;
|
||||
Config.GoogleTokenIssued = null;
|
||||
Config.GoogleTokenIssued = null;
|
||||
return Task.Delay(0);
|
||||
}
|
||||
|
||||
public Task StoreAsync<T>(string key, T value)
|
||||
{
|
||||
JObject json = JObject.Parse(NewtonsoftJsonSerializer.Instance.Serialize(value));
|
||||
|
||||
JToken accessToken = json.SelectToken(@"access_token");
|
||||
if (accessToken != null)
|
||||
Config.GoogleAccessToken = (string)accessToken;
|
||||
|
||||
JToken tokenType = json.SelectToken(@"token_type");
|
||||
if (tokenType != null)
|
||||
Config.GoogleTokenType = (string)tokenType;
|
||||
|
||||
JToken expiresIn = json.SelectToken(@"expires_in");
|
||||
if (expiresIn != null)
|
||||
Config.GoogleTokenExpires = (long?)expiresIn;
|
||||
|
||||
JToken refreshToken = json.SelectToken(@"refresh_token");
|
||||
if (refreshToken != null)
|
||||
Config.GoogleRefreshToken = (string)refreshToken;
|
||||
|
||||
JToken tokenIssued = json.SelectToken(@"Issued");
|
||||
if (refreshToken != null)
|
||||
Config.GoogleTokenIssued = (string)tokenIssued;
|
||||
|
||||
return Task.Delay(0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,6 @@
|
|||
using Google.Apis.Auth.OAuth2;
|
||||
using Google.Apis.Drive.v3;
|
||||
using Google.Apis.Services;
|
||||
using Renci.SshNet;
|
||||
using Renci.SshNet;
|
||||
using Renci.SshNet.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
@ -17,7 +13,6 @@ using System.Text;
|
|||
using System.Threading;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
using GFile = Google.Apis.Drive.v3.Data.File;
|
||||
|
||||
namespace BackupManager
|
||||
{
|
||||
|
@ -25,8 +20,6 @@ namespace BackupManager
|
|||
{
|
||||
public readonly static Stopwatch sw = new Stopwatch();
|
||||
|
||||
private const string FOLDER_MIME = @"application/vnd.google-apps.folder";
|
||||
|
||||
private const string CONFIG_NAME = @"FlashiiBackupManager.v1.xml";
|
||||
|
||||
public static bool IsWindows
|
||||
|
@ -47,7 +40,6 @@ namespace BackupManager
|
|||
CONFIG_NAME
|
||||
);
|
||||
|
||||
private static DriveService DriveService;
|
||||
private static object BackupStorage;
|
||||
|
||||
private static SftpClient SFTP;
|
||||
|
@ -113,22 +105,6 @@ namespace BackupManager
|
|||
|
||||
switch (Config.StorageMethod)
|
||||
{
|
||||
case StorageMethod.GoogleDrive:
|
||||
UserCredential uc = GoogleAuthenticate(
|
||||
new ClientSecrets
|
||||
{
|
||||
ClientId = Config.GoogleClientId,
|
||||
ClientSecret = Config.GoogleClientSecret,
|
||||
},
|
||||
new[] {
|
||||
DriveService.Scope.Drive,
|
||||
DriveService.Scope.DriveFile,
|
||||
}
|
||||
);
|
||||
|
||||
CreateDriveService(uc);
|
||||
break;
|
||||
|
||||
case StorageMethod.Sftp:
|
||||
if (string.IsNullOrWhiteSpace(Config.SftpHost) || string.IsNullOrWhiteSpace(Config.SftpUsername))
|
||||
{
|
||||
|
@ -185,10 +161,6 @@ namespace BackupManager
|
|||
|
||||
switch (f)
|
||||
{
|
||||
case GFile fgf:
|
||||
Log($@"MySQL dump uploaded: {fgf.Name} ({fgf.Id})");
|
||||
break;
|
||||
|
||||
default:
|
||||
Log($@"MySQL dump uploaded.");
|
||||
break;
|
||||
|
@ -216,10 +188,6 @@ namespace BackupManager
|
|||
|
||||
switch (f)
|
||||
{
|
||||
case GFile fgf:
|
||||
Log($@"Misuzu data uploaded: {fgf.Name} ({fgf.Id})");
|
||||
break;
|
||||
|
||||
default:
|
||||
Log($@"Misuzu data uploaded.");
|
||||
break;
|
||||
|
@ -281,18 +249,6 @@ namespace BackupManager
|
|||
|
||||
switch (BackupStorage)
|
||||
{
|
||||
case GFile gfile:
|
||||
FilesResource.CreateMediaUpload request = DriveService.Files.Create(new GFile
|
||||
{
|
||||
Name = name,
|
||||
Parents = new List<string> {
|
||||
gfile.Id,
|
||||
},
|
||||
}, stream, type);
|
||||
request.Fields = @"id, name";
|
||||
request.Upload();
|
||||
return request.ResponseBody;
|
||||
|
||||
case string scpName:
|
||||
SFTP.UploadFile(stream, scpName + @"/" + name);
|
||||
break;
|
||||
|
@ -413,57 +369,10 @@ namespace BackupManager
|
|||
return output;
|
||||
}
|
||||
|
||||
public static UserCredential GoogleAuthenticate(ClientSecrets cs, string[] scopes)
|
||||
{
|
||||
Log(@"Authenticating with Google...");
|
||||
return GoogleWebAuthorizationBroker.AuthorizeAsync(
|
||||
cs,
|
||||
scopes,
|
||||
@"user",
|
||||
CancellationToken.None,
|
||||
new GoogleDatastore(Config),
|
||||
new PromptCodeReceiver()
|
||||
).Result;
|
||||
}
|
||||
|
||||
public static void CreateDriveService(UserCredential uc)
|
||||
{
|
||||
Log(@"Creating Google Drive service...");
|
||||
DriveService = new DriveService(new BaseClientService.Initializer()
|
||||
{
|
||||
HttpClientInitializer = uc,
|
||||
ApplicationName = @"Flashii Backup Manager",
|
||||
});
|
||||
}
|
||||
|
||||
public static void GetBackupStorage(string name = null)
|
||||
{
|
||||
switch (Config.StorageMethod)
|
||||
{
|
||||
case StorageMethod.GoogleDrive:
|
||||
name = name ?? Config.GoogleBackupDirectory;
|
||||
Log(@"Getting backup folder...");
|
||||
FilesResource.ListRequest lr = DriveService.Files.List();
|
||||
lr.Q = $@"name = '{name}' and mimeType = '{FOLDER_MIME}'";
|
||||
lr.PageSize = 1;
|
||||
lr.Fields = @"files(id)";
|
||||
GFile backupFolder = lr.Execute().Files.FirstOrDefault();
|
||||
|
||||
if (backupFolder == null)
|
||||
{
|
||||
Log(@"Backup folder doesn't exist yet, creating it...");
|
||||
FilesResource.CreateRequest dcr = DriveService.Files.Create(new GFile
|
||||
{
|
||||
Name = name,
|
||||
MimeType = FOLDER_MIME,
|
||||
});
|
||||
dcr.Fields = @"id";
|
||||
backupFolder = dcr.Execute();
|
||||
}
|
||||
|
||||
BackupStorage = backupFolder;
|
||||
break;
|
||||
|
||||
case StorageMethod.Sftp:
|
||||
string directory = (BackupStorage = name ?? Config.SftpBackupDirectoryPath) as string;
|
||||
try
|
||||
|
|
Reference in a new issue