An open API service indexing awesome lists of open source software.

https://github.com/sitholewb/files.entityframeworkcore.extensions

This is a library for storing files in small chunks on sql using EntityFrameworkCore. Works well with Entity Framework as an extension.
https://github.com/sitholewb/files.entityframeworkcore.extensions

blob-storage bytes entity-framework-core extensions file-upload files images storage

Last synced: about 1 year ago
JSON representation

This is a library for storing files in small chunks on sql using EntityFrameworkCore. Works well with Entity Framework as an extension.

Awesome Lists containing this project

README

          

# Files.EntityFrameworkCore.Extensions

This is a library for storing files in small chunks on sql using EntityFrameworkCore.
Works well with **Entity Framework** as an extension.

# Get Started

```nuget
Install-Package Files.EntityFrameworkCore.Extensions
```

# Example
### More examples found inside the repository
```C#

public class UserImage : IFileEntity
{
public Guid Id { get; set; }
public Guid FileId { get; set; }
public string Name { get; set; }
public string MimeType { get; set; }
public DateTimeOffset TimeStamp { get; set; }
public Guid? NextId { get; set; }
public int ChunkBytesLength { get; set; }
public long TotalBytesLength { get; set; }
public byte[] Data { get; set; }
}

public class UploadFileIdCommand
{
public Guid? FileId { get; set; }
}

public class UploadCommand
{
public IFormFile File { get; set; }
}

public class WebApiContext : DbContext
{
public WebApiContext(DbContextOptions options)
: base(options)
{
}
public DbSet UserImage { get; set; }
}

[Route("api/user-images")]
[ApiController]
public class UserImagesController : ControllerBase
{
private readonly WebApiContext _context;

public UserImagesController(WebApiContext context)
{
_context = context;
}

[HttpPost]
[DisableRequestSizeLimit]
public async Task> UploadFile([FromForm] UploadCommand uploadCommand)
{
var file = uploadCommand.File;
if (file.Length > 0)
{
var fileDetails = await _context.SaveFileAsync(file.OpenReadStream(), file.FileName, file.ContentType);
return Ok(fileDetails);
}
else
{
return BadRequest("File is required.");
}
}

[HttpPost("other-file")]
public async Task> UploadOtherFile([FromBody] UploadFileIdCommand command)
{
//The @"appsettings.json" is a path to any file you will like to save
var fileDetails = await _context.SaveFileAsync(@"appsettings.json", command?.FileId);
//Save will be auto called on every chunk addition so that memory usage remain low, i.e. await _context.SaveChangesAsync();
return Ok(fileDetails);
}

[HttpGet("{id}/download")]
public async Task DownLoadFile(Guid id)
{
var fileDetails = await _context.GetFileInfoAsync(id);
var stream = new MemoryStream();
await _context.DownloadFileToStreamAsync(id, stream);
return File(stream, fileDetails.MimeType, fileDetails.Name);
}

[HttpGet("{id}/view")]
public async Task DownloadView(Guid id)
{
var fileDetails = await _context.GetFileInfoAsync(id);
var stream = new MemoryStream();
await _context.DownloadFileToStreamAsync(id, stream);
return new FileStreamResult(stream, fileDetails.MimeType);
}

[HttpDelete("{id}")]
public async Task DeleteUserImage(Guid id)
{
if (_context.UserImage == null)
{
return NotFound();
}
var userImage = await _context.UserImage.FindAsync(id);
if (userImage == null)
{
return NotFound();
}

await _context.DeleteFileAsync(id);
//Save will be auto called on every chunk deletion so that memory usage remain low, i.e. await _context.SaveChangesAsync();

return NoContent();
}
}

```