Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysql storage #777

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/BaGet.Core/Entities/IPackageContentsContext.cs
@@ -0,0 +1,13 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace BaGet.Core
{
public interface IPackageContentsContext
{
DbSet<PackageContents> PackageContents { get; set; }

Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
}
11 changes: 11 additions & 0 deletions src/BaGet.Core/Entities/PackageContents.cs
@@ -0,0 +1,11 @@
namespace BaGet.Core
{
public class PackageContents
{
public int Key { get; set; }

public string Path { get; set; }

public byte[] Data { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/BaGet.Core/Extensions/BaGetApplicationExtensions.cs
Expand Up @@ -22,6 +22,12 @@ public static BaGetApplication AddFileStorage(this BaGetApplication app)
return app;
}

public static BaGetApplication AddMySqlStorage(this BaGetApplication app)
{
app.Services.TryAddTransient<IStorageService>(provider => provider.GetRequiredService<DatabaseStorageService>());
return app;
}

public static BaGetApplication AddNullStorage(this BaGetApplication app)
{
app.Services.TryAddTransient<IStorageService>(provider => provider.GetRequiredService<NullStorageService>());
Expand Down
6 changes: 6 additions & 0 deletions src/BaGet.Core/Extensions/DependencyInjectionExtensions.cs
Expand Up @@ -100,6 +100,7 @@ private static void AddBaGetServices(this IServiceCollection services)

services.TryAddTransient<DatabaseSearchService>();
services.TryAddTransient<FileStorageService>();
services.TryAddTransient<DatabaseStorageService>();
services.TryAddTransient<PackageService>();
services.TryAddTransient<V2UpstreamClient>();
services.TryAddTransient<V3UpstreamClient>();
Expand Down Expand Up @@ -133,6 +134,11 @@ private static void AddDefaultProviders(this IServiceCollection services)
return provider.GetRequiredService<FileStorageService>();
}

if (configuration.HasStorageType("mysql"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized this should perhaps be "database" not "mysql". What do you think @loic-sharma?

{
return provider.GetRequiredService<DatabaseStorageService>();
}

if (configuration.HasStorageType("null"))
{
return provider.GetRequiredService<NullStorageService>();
Expand Down
73 changes: 73 additions & 0 deletions src/BaGet.Core/Storage/DatabaseStorageService.cs
@@ -0,0 +1,73 @@
using System.Linq;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace BaGet.Core
{
public class DatabaseStorageService : IStorageService
{
private readonly IPackageContentsContext _context;

public DatabaseStorageService(IPackageContentsContext context)
{
if (context == null) throw new ArgumentException(nameof(context));

_context = context;
}

public async Task DeleteAsync(string path, CancellationToken cancellationToken = default)
{
var contents = await _context.PackageContents.SingleOrDefaultAsync(p => p.Path == path, cancellationToken);
if (contents != null)
{
_context.PackageContents.Remove(contents);
await _context.SaveChangesAsync(cancellationToken);
}
}

public async Task<Stream> GetAsync(string path, CancellationToken cancellationToken = default)
{
var contents = await _context.PackageContents.SingleOrDefaultAsync(p => p.Path == path, cancellationToken);
if (contents == null)
{
throw new Exception($"PackageContents record not found for path: {path}");
}
var ms = new MemoryStream(contents.Data);
return ms;
}

public Task<Uri> GetDownloadUriAsync(string path, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public async Task<StoragePutResult> PutAsync(string path, Stream content, string contentType, CancellationToken cancellationToken = default)
{
byte[] newData;
using (var binaryReader = new BinaryReader(content))
{
newData = binaryReader.ReadBytes((int)content.Length);
}

var existingContents = await _context.PackageContents.SingleOrDefaultAsync(p => p.Path == path, cancellationToken);
if (existingContents != null)
{
return existingContents.Data.SequenceEqual(newData)
? StoragePutResult.AlreadyExists
: StoragePutResult.Success;
}

_context.PackageContents.Add(new PackageContents
{
Path = path,
Data = newData,
});
await _context.SaveChangesAsync(cancellationToken);

return StoragePutResult.Success;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.