forked from kc3hack/2024_H
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#41 S3へのアップロードを実装
- Loading branch information
Showing
31 changed files
with
263 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace KoeBook.Core.Contracts.Services; | ||
|
||
public interface IS3UploadService | ||
{ | ||
ValueTask<string> UploadFileAsync(string filePath, string title, CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
using NAudio.Wave; | ||
using System.IO; | ||
using NAudio.Wave; | ||
|
||
namespace KoeBook.Epub.Models; | ||
|
||
public sealed class Audio | ||
public sealed class Audio(TimeSpan totalTIme, string tempFilePath) | ||
{ | ||
public TimeSpan TotalTime { get; } | ||
private readonly byte[] _mp3Data; | ||
public TimeSpan TotalTime { get; } = totalTIme; | ||
public string TempFilePath { get; } = tempFilePath; | ||
|
||
public Audio(byte[] mp3Data) | ||
public FileStream GetStream() | ||
{ | ||
_mp3Data = mp3Data; | ||
using var ms = new MemoryStream(); | ||
ms.Write(_mp3Data.AsSpan()); | ||
ms.Flush(); | ||
ms.Position = 0; | ||
using var reader = new Mp3FileReader(ms); | ||
TotalTime = reader.TotalTime; | ||
} | ||
|
||
public MemoryStream GetStream() | ||
{ | ||
return new MemoryStream(_mp3Data); | ||
return new FileStream(TempFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Amazon.S3; | ||
using Amazon.S3.Transfer; | ||
using KoeBook.Core.Contracts.Services; | ||
|
||
namespace KoeBook.Core.Services; | ||
|
||
public class S3UploadService(IAmazonS3 s3Client) : IS3UploadService | ||
{ | ||
private readonly IAmazonS3 _s3Client = s3Client; | ||
|
||
public async ValueTask<string> UploadFileAsync(string filePath, string title, CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
// 設定に移すのが面倒なので固定値 | ||
const string S3BucketName = "koebook-gakusai-storage"; | ||
var guid = Guid.NewGuid(); | ||
var fileTransferUtility = new TransferUtility(_s3Client); | ||
await fileTransferUtility.UploadAsync(filePath, S3BucketName, $"{guid}/{title}.epub", cancellationToken); | ||
|
||
return $"http://storage.koebook.oucc.org/{guid}/{Uri.EscapeDataString(title)}.epub"; | ||
} | ||
catch (AmazonS3Exception e) | ||
{ | ||
throw new EbookException(ExceptionType.S3UploadFailed, innerException: e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.