Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #727 from keiji/improve_log_share
Browse files Browse the repository at this point in the history
Improve log share
  • Loading branch information
cocoa-dev003 authored Mar 3, 2022
2 parents eb43b40 + 3e03ccb commit d735fa0
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 234 deletions.
6 changes: 3 additions & 3 deletions Covid19Radar/Covid19Radar/Services/Logs/ILogFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public interface ILogFileService
{
// Log upload
string CreateLogId();
string LogUploadingFileName(string logId);
bool CreateLogUploadingFileToTmpPath(string logUploadingFileName);
bool CopyLogUploadingFileToPublicPath(string logUploadingFileName);
string CreateZipFileName(string logId);
string CreateZipFile(string fileName);
string CopyLogUploadingFileToPublicPath(string logPath);
bool DeleteAllLogUploadingFiles();

// Log rotate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Covid19Radar.Services.Logs
{
public interface ILogUploadService
{
Task<bool> UploadAsync(string zipFileName, string sasToken);
Task<bool> UploadAsync(string zipFilePath, string sasToken);
}
}
52 changes: 30 additions & 22 deletions Covid19Radar/Covid19Radar/Services/Logs/LogFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class LogFileService : ILogFileService
{
#region Static Fields

private static readonly string logUploadFilePrefix = "cocoa_log_";
private static readonly string logUploadFileExtension = "zip";
private static readonly string logFilePrefix = "cocoa_log_";
private static readonly string logFileExtension = "zip";

#endregion

Expand Down Expand Up @@ -45,12 +45,12 @@ IBackupAttributeService backupAttributeService

public string CreateLogId() => Guid.NewGuid().ToString();

public string LogUploadingFileName(string logId)
public string CreateZipFileName(string logId)
{
return logUploadFilePrefix + logId + "." + logUploadFileExtension;
return logFilePrefix + logId + "." + logFileExtension;
}

public bool CreateLogUploadingFileToTmpPath(string logUploadingFileName)
public string CreateZipFile(string fileName)
{
loggerService.StartMethod();
try
Expand All @@ -59,42 +59,50 @@ public bool CreateLogUploadingFileToTmpPath(string logUploadingFileName)
var logFiles = Directory.GetFiles(logsDirPath, logPathService.LogFileWildcardName);
if (logFiles.Length == 0)
{
loggerService.EndMethod();
return false;
return null;
}
ZipFile.CreateFromDirectory(logsDirPath, Path.Combine(logPathService.LogUploadingTmpPath, logUploadingFileName));
loggerService.EndMethod();
return true;

var zipFilePath = Path.Combine(logPathService.LogUploadingTmpPath, fileName);
ZipFile.CreateFromDirectory(logsDirPath, zipFilePath);

return zipFilePath;
}
catch (Exception)
catch (Exception exception)
{
loggerService.Exception("Failed to create uploading file", exception);
return null;
}
finally
{
loggerService.Error("Failed to create uploading file");
loggerService.EndMethod();
return false;
}
}

public bool CopyLogUploadingFileToPublicPath(string logUploadingFileName)
public string CopyLogUploadingFileToPublicPath(string logPath)
{
loggerService.StartMethod();
try
{
var logFileName = Path.GetFileName(logPath);
var tmpPath = logPathService.LogUploadingTmpPath;
var publicPath = logPathService.LogUploadingPublicPath;
if (string.IsNullOrEmpty(tmpPath) || string.IsNullOrEmpty(publicPath))
{
loggerService.EndMethod();
return false;
return null;
}
File.Copy(Path.Combine(tmpPath, logUploadingFileName), Path.Combine(publicPath, logUploadingFileName), true);
loggerService.EndMethod();
return true;
var destPath = Path.Combine(publicPath, logFileName);
File.Copy(logPath, destPath, true);

return destPath;
}
catch (Exception)
catch (Exception exception)
{
loggerService.Exception("Failed to copy log file", exception);
return null;
}
finally
{
loggerService.Error("Failed to copy log file");
loggerService.EndMethod();
return false;
}
}

Expand Down
6 changes: 2 additions & 4 deletions Covid19Radar/Covid19Radar/Services/Logs/LogUploadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using System.IO;
using System.Threading.Tasks;

namespace Covid19Radar.Services.Logs
Expand All @@ -24,7 +23,7 @@ public LogUploadService(
this.storageService = storageService;
}

public async Task<bool> UploadAsync(string zipFileName, string sasToken)
public async Task<bool> UploadAsync(string zipFilePath, string sasToken)
{
loggerService.StartMethod();

Expand All @@ -34,14 +33,13 @@ public async Task<bool> UploadAsync(string zipFileName, string sasToken)
{
// Upload to storage.
var logTmpPath = logPathService.LogUploadingTmpPath;
var logZipPath = Path.Combine(logTmpPath, zipFileName);

var setting = AppSettings.Instance;
var endpoint = setting.LogStorageEndpoint;
var uploadPath = setting.LogStorageContainerName;
var accountName = setting.LogStorageAccountName;

var uploadResult = await storageService.UploadAsync(endpoint, uploadPath, accountName, sasToken, logZipPath);
var uploadResult = await storageService.UploadAsync(endpoint, uploadPath, accountName, sasToken, zipFilePath);
if (!uploadResult)
{
throw new Exception("Failed to upload to storage.");
Expand Down
100 changes: 97 additions & 3 deletions Covid19Radar/Covid19Radar/ViewModels/HelpPage/InqueryPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Threading.Tasks;
using Acr.UserDialogs;
using Covid19Radar.Resources;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
Expand All @@ -17,15 +18,26 @@ namespace Covid19Radar.ViewModels
public class InqueryPageViewModel : ViewModelBase
{
private readonly ILoggerService loggerService;
private readonly ILogFileService logFileService;
private readonly ILogPathService logPathService;

private readonly IEssentialsService essentialsService;

public Func<string, BrowserLaunchMode, Task> BrowserOpenAsync = Browser.OpenAsync;
public Func<string, string, string[], Task> ComposeEmailAsync { get; set; } = Email.ComposeAsync;

public InqueryPageViewModel(INavigationService navigationService, ILoggerService loggerService, IEssentialsService essentialsService) : base(navigationService)
public InqueryPageViewModel(
INavigationService navigationService,
ILoggerService loggerService,
ILogFileService logFileService,
ILogPathService logPathService,
IEssentialsService essentialsService
) : base(navigationService)
{
Title = AppResources.InqueryPageTitle;
this.loggerService = loggerService;
this.logFileService = logFileService;
this.logPathService = logPathService;
this.essentialsService = essentialsService;
}

Expand All @@ -43,9 +55,79 @@ public InqueryPageViewModel(INavigationService navigationService, ILoggerService
{
loggerService.StartMethod();

_ = await NavigationService.NavigateAsync(nameof(SendLogConfirmationPage));
try
{
UserDialogs.Instance.ShowLoading(AppResources.Processing);

loggerService.EndMethod();
var (logId, zipFilePath) = CreateZipFile();

UserDialogs.Instance.HideLoading();

if (zipFilePath is null)
{
// Failed to create ZIP file
await UserDialogs.Instance.AlertAsync(
AppResources.FailedMessageToGetOperatingInformation,
AppResources.Error,
AppResources.ButtonOk);
return;
}

loggerService.Info($"zipFilePath: {zipFilePath}");

INavigationParameters navigationParameters
= SendLogConfirmationPage.BuildNavigationParams(logId, zipFilePath);

_ = await NavigationService.NavigateAsync(nameof(SendLogConfirmationPage), navigationParameters);
}
finally
{
loggerService.EndMethod();
}
});

public Command OnClickShareLogCommand => new Command(async () =>
{
loggerService.StartMethod();

try
{
UserDialogs.Instance.ShowLoading(AppResources.Processing);

var (logId, zipFilePath) = CreateZipFile();

UserDialogs.Instance.HideLoading();

if (zipFilePath is null)
{
// Failed to create ZIP file
await UserDialogs.Instance.AlertAsync(
AppResources.FailedMessageToGetOperatingInformation,
AppResources.Error,
AppResources.ButtonOk);
return;
}

loggerService.Info($"zipFilePath: {zipFilePath}");

string sharePath = logFileService.CopyLogUploadingFileToPublicPath(zipFilePath);

try
{
await Share.RequestAsync(new ShareFileRequest
{
File = new ShareFile(sharePath)
});
}
catch (NotImplementedInReferenceAssemblyException exception)
{
loggerService.Exception("NotImplementedInReferenceAssemblyException", exception);
}
}
finally
{
loggerService.EndMethod();
}
});

public Command OnClickEmailCommand => new Command(async () =>
Expand Down Expand Up @@ -85,5 +167,17 @@ private string CreateInquiryMailBody()
+ AppResources.InquiryMailOSVersionTitle + essentialsService.PlatformVersion + "\r\n"
+ AppResources.InquiryMailAppVersionTitle + essentialsService.AppVersion;
}

private (string, string) CreateZipFile()
{
string logId = logFileService.CreateLogId();
string zipFileName = logFileService.CreateZipFileName(logId);

logFileService.Rotate();

var zipFilePath = logFileService.CreateZipFile(zipFileName);

return (logId, zipFilePath);
}
}
}
Loading

0 comments on commit d735fa0

Please sign in to comment.