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

Media files #49

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Controllers/VideoTourController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using SimpleWebAppReact.Entities;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using SimpleWebAppReact.Services;

namespace SimpleWebAppReact.Controllers
Expand All @@ -15,11 +17,13 @@
{
private readonly ILogger<VideoTourController> _logger;
private readonly IMongoCollection<VideoTour>? _videoTours;
private readonly IGridFSBucket _videosBucket;

public VideoTourController(ILogger<VideoTourController> logger, MongoDbService mongoDbService)
{
_logger = logger;
_videoTours = mongoDbService.Database?.GetCollection<VideoTour>("videoTour");
_videosBucket = new GridFSBucket(mongoDbService.Database);
}

/// <summary>
Expand All @@ -40,7 +44,7 @@
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<ActionResult<VideoTour?>> GetById(string id)

Check warning on line 47 in Controllers/VideoTourController.cs

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (!ModelState.IsValid) {
return BadRequest(ModelState);
Expand All @@ -52,15 +56,42 @@
return videoTour is not null ? Ok(videoTour) : NotFound();
}

[HttpGet("{id}/file")]
public async Task<ActionResult> GetFileById(string id)
{
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, id);
var videoTour = (await _videoTours.FindAsync(filter)).FirstOrDefault();

if (videoTour is null)
{
return NotFound();
}

var videoId = ObjectId.Parse(videoTour.FileId);
await using var downloadStream = await _videosBucket.OpenDownloadStreamAsync(videoId);
return File(downloadStream, "video/mp4");
}

/// <summary>
/// adds videoTour entry to table
/// </summary>
/// <param name="videoTour"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult> Post(VideoTour videoTour)
public async Task<ActionResult> Post([FromQuery] VideoTour videoTour, IFormFile file)
{
if (file.ContentType != "video/mp4")
{
return BadRequest("File must be an mp4");
}

var fileStream = file.OpenReadStream();
var objectId = await _videosBucket.UploadFromStreamAsync(videoTour.Id, fileStream);
fileStream.Close();

videoTour.FileId = objectId.ToString();

await _videoTours.InsertOneAsync(videoTour);

Check warning on line 94 in Controllers/VideoTourController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
return CreatedAtAction(nameof(GetById), new { id = videoTour.Id }, videoTour);
}

Expand All @@ -73,7 +104,7 @@
public async Task<ActionResult> Update(VideoTour videoTour)
{
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, videoTour.Id);
await _videoTours.ReplaceOneAsync(filter, videoTour);

Check warning on line 107 in Controllers/VideoTourController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
return Ok();
}

Expand All @@ -86,7 +117,18 @@
public async Task<ActionResult> Delete(string id)
{
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, id);
var videoTour = (await _videoTours.FindAsync(filter)).FirstOrDefault();

if (videoTour is null)
{
return NotFound();
}

await _videoTours.DeleteOneAsync(filter);

var objectId = ObjectId.Parse(videoTour.FileId);
await _videosBucket.DeleteAsync(objectId);

return Ok();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Entities/VideoTour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class VideoTour
[BsonElement("title"), BsonRepresentation(BsonType.String)]
public string? Title { get; set; }

[BsonElement("url"), BsonRepresentation(BsonType.String)]
public string? Url { get; set; }
[BsonElement("fileId"), BsonRepresentation(BsonType.String)]
public string? FileId { get; set; }

[BsonElement("length"), BsonRepresentation(BsonType.Double)]
public Double? Length { get; set; }
Expand Down
1 change: 1 addition & 0 deletions SimpleWebAppReact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.33" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.28.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
Loading