Skip to content

Commit 5ca62d1

Browse files
authored
Feat/add dossier zip password (#29)
* implemented locking zip file within another with password * Implemented file zipping and protected with password when dossier closed
1 parent af0cf92 commit 5ca62d1

4 files changed

Lines changed: 59 additions & 37 deletions

File tree

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
</PropertyGroup>
8-
9-
<ItemGroup>
10-
<PackageReference Include="Dropbox.Api" Version="6.27.0" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5">
13-
<PrivateAssets>all</PrivateAssets>
14-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15-
</PackageReference>
16-
</ItemGroup>
17-
18-
<ItemGroup>
19-
<ProjectReference Include="..\DropboxSync.Helpers\DropboxSync.Helpers.csproj" />
20-
</ItemGroup>
21-
22-
</Project>
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<ImplicitUsings>enable</ImplicitUsings>
5+
<Nullable>enable</Nullable>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Dropbox.Api" Version="6.27.0"/>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5"/>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="DotNetZip" Version="1.16.0"/>
15+
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="..\DropboxSync.Helpers\DropboxSync.Helpers.csproj"/>
18+
</ItemGroup>
19+
</Project>

DropboxSync.BLL/IServices/IFileService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace DropboxSync.BLL.IServices
1010
public record SavedFile(string RelativePath, string ContentType, string FileName, long FileSize);
1111
public interface IFileService
1212
{
13-
Task<SavedFile?> DownloadFile(string fileId);
13+
Task<SavedFile?> DownloadFile(string fileId, bool lockFile = false);
1414
bool Delete(string fileName);
1515
}
1616
}

DropboxSync.BLL/Services/FileService.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
using DropboxSync.BLL.IServices;
44
using Microsoft.Extensions.Logging;
55
using Newtonsoft.Json;
6-
using Newtonsoft.Json.Linq;
7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
106
using System.Net.Http.Headers;
11-
using System.Net.Http.Json;
12-
using System.Text;
13-
using System.Threading.Tasks;
7+
using Ionic.Zip;
148

159
namespace DropboxSync.BLL.Services
1610
{
@@ -58,7 +52,7 @@ public FileService(ILogger<FileService> logger)
5852
/// </summary>
5953
/// <param name="fileId">The ID of the file to download</param>
6054
/// <returns><see cref="SavedFile"/> object if download and save was successfull. <c>null</c> Otherwise</returns>
61-
public async Task<SavedFile?> DownloadFile(string fileId)
55+
public async Task<SavedFile?> DownloadFile(string fileId, bool lockFile)
6256
{
6357
if (string.IsNullOrEmpty(fileId)) throw new ArgumentNullException(nameof(fileId));
6458

@@ -131,14 +125,45 @@ public FileService(ILogger<FileService> logger)
131125
return finalOutput;
132126
}
133127

134-
string filePath = Path.Join(FILE_DOWNLOAD_DIR, string.Join('-', fileId, fileName));
128+
string filePath;
129+
FileInfo fileInfo;
135130

136-
await File.WriteAllBytesAsync(filePath, fileData);
137-
FileInfo fileInfo = new FileInfo(filePath);
138-
if (!fileInfo.Exists)
131+
if (lockFile)
139132
{
140-
_logger.LogError("{date} | File at path \"{filePath}\" doesn't exist!", DateTime.Now, filePath);
141-
return finalOutput;
133+
string tmpFilePath = Path.Join(FILE_DOWNLOAD_DIR, fileName);
134+
filePath = Path.Join(FILE_DOWNLOAD_DIR, string.Join('-', fileId, fileName));
135+
136+
await File.WriteAllBytesAsync(tmpFilePath, fileData);
137+
fileInfo = new FileInfo(tmpFilePath);
138+
if (!fileInfo.Exists)
139+
{
140+
_logger.LogError("{date} | File at path \"{filePath}\" doesn't exist!", DateTime.Now, tmpFilePath);
141+
return finalOutput;
142+
}
143+
144+
string zipPassword = Environment.GetEnvironmentVariable("ZIP_PASSWORD") ??
145+
"1234";
146+
147+
using (ZipFile zip = new ZipFile())
148+
{
149+
zip.Password = zipPassword;
150+
zip.AddFile(tmpFilePath, "");
151+
zip.Save(filePath);
152+
}
153+
154+
File.Delete(tmpFilePath);
155+
}
156+
else
157+
{
158+
filePath = Path.Join(FILE_DOWNLOAD_DIR, string.Join('-', fileId, fileName));
159+
160+
await File.WriteAllBytesAsync(filePath, fileData);
161+
fileInfo = new FileInfo(filePath);
162+
if (!fileInfo.Exists)
163+
{
164+
_logger.LogError("{date} | File at path \"{filePath}\" doesn't exist!", DateTime.Now, filePath);
165+
return finalOutput;
166+
}
142167
}
143168

144169
long fileSize = fileInfo.Length;

DropboxSync.UIL/Managers/DossierManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public bool CloseDossier(DossierCloseModel model)
5656
return false;
5757
}
5858

59-
SavedFile? localSaveResult = AsyncHelper.RunSync(() => _fileService.DownloadFile(model.UploadId));
59+
SavedFile? localSaveResult = AsyncHelper.RunSync(() => _fileService.DownloadFile(model.UploadId, true));
6060

6161
if (localSaveResult is null)
6262
{

0 commit comments

Comments
 (0)