Skip to content

Commit

Permalink
Added local disk as a storage (#69)
Browse files Browse the repository at this point in the history
* Added local disk as a storage

* Fix for chromium version

Co-authored-by: Savpek <[email protected]>
  • Loading branch information
juha-heikkinen and savpek authored Sep 22, 2021
1 parent ae967af commit f2dfe40
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Config/LocalConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Pdf.Storage.Pdf.Config
{
public class LocalStorageConfig
{
public string Folder { get; set; }
}
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ! IMPORTANT: Keep chromium version synced with version from package 'PuppeteerSharp'
# and match it with from https://pkgs.alpinelinux.org/packages
ARG chromium_version=91.0.4472.101-r0
ARG chromium_version=93.0.4577.82-r1

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.100-alpine3.10 as dotnetBuild
ARG chromium_version
Expand Down
3 changes: 2 additions & 1 deletion Pdf.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Protacon.NetCore.WebApi.Util" Version="0.0.4" />
<PackageReference Include="RabbitMQ.Client" Version="5.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="System.IO.Abstractions" Version="13.2.47" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.0" />
Expand All @@ -55,7 +56,7 @@
<!-- When you update PuppeteerSharp you must also find corresponding version
in alpine at Dockerfile. Alpine limits available chromium versions available so don't update
this without check them out. Chromium version must be about same as puppeteer expects -->
<PackageReference Include="PuppeteerSharp" Version="4.0.0" />
<PackageReference Include="PuppeteerSharp" Version="5.0.0" />

<!-- Remove this once testing library is updated -->
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" />
Expand Down
59 changes: 59 additions & 0 deletions Pdf/PdfStores/LocalStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections.Generic;
using System.IO.Abstractions;
using Microsoft.Extensions.Options;
using Pdf.Storage.Pdf;
using Pdf.Storage.Pdf.Config;
using Pdf.Storage.Pdf.PdfStores;

namespace Pdf.Storage.Hangfire
{
public class LocalStorage : IStorage
{
private readonly IFileSystem _fileSystem;
private readonly string _folder;

public LocalStorage(IFileSystem fs, IOptions<LocalStorageConfig> options)
{
_fileSystem = fs;

_folder = options.Value.Folder ?? "/tmp/";
if (_folder[^1] != '/')
_folder += "/";
}

public void AddOrReplace(StorageData storageData)
{
if (_fileSystem.File.Exists(GetPath(storageData.StorageFileId)))
_fileSystem.File.Delete(GetPath(storageData.StorageFileId));

_fileSystem.File.WriteAllBytes(GetPath(storageData.StorageFileId), storageData.Data);
}

public StorageData Get(StorageFileId storageFileId)
{
if (!_fileSystem.File.Exists(GetPath(storageFileId)))
return null;

var bytes = _fileSystem.File.ReadAllBytes(GetPath(storageFileId));
var result = new StorageData(storageFileId, bytes);

return result;
}

public void Remove(StorageFileId storageFileId)
{
if (_fileSystem.File.Exists(GetPath(storageFileId)))
_fileSystem.File.Delete(GetPath(storageFileId));
}

private string GetKey(StorageFileId storageFileId)
{
return $"{storageFileId.Group}_{storageFileId.Id}.{storageFileId.Extension}";
}

private string GetPath(StorageFileId storageFileId)
{
return $"{_folder}{GetKey(storageFileId)}";
}
}
}
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ PDF storage supports Azure storage accounts as storage.
}
```

### Local storage

Use local disk as storage.

```json
{
"PdfStorageType": "local",
"LocalStorage": {
"Folder": "/tmp"
}
}
```

## Migrations

Tooling requires `dotnet-ef` available, so run:
Expand Down
6 changes: 6 additions & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Pdf.Storage.Pdf.PdfStores;
using Pdf.Storage.Migrations;
using Hangfire.Dashboard;
using System.IO.Abstractions;

namespace Pdf.Storage
{
Expand Down Expand Up @@ -137,6 +138,11 @@ public void ConfigureServices(IServiceCollection services)
case "inMemory":
services.AddSingleton<IStorage, InMemoryPdfStorage>();
break;
case "local":
services.Configure<LocalStorageConfig>(Configuration.GetSection("LocalStorage"));
services.AddSingleton<IFileSystem, FileSystem>();
services.AddSingleton<IStorage, LocalStorage>();
break;
default:
throw new InvalidOperationException($"Unknown PdfStorageType from configuration '{Configuration["PdfStorageType"]}'");
}
Expand Down
3 changes: 3 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"GoogleBucketName": "pdf-storage-master",
"GoogleAuthFile": "google.key.json"
},
"LocalStorage": {
"Folder": "/tmp"
},
"RabbitMq": {
"Host": "localhost"
},
Expand Down

0 comments on commit f2dfe40

Please sign in to comment.