#7 logic for simpe mp3 + xml uploads is nearly complete (database objects are not yet produced and code is not tested yet)

This commit is contained in:
Hamid Reza Mohammadi 2020-09-11 17:05:17 +04:30
parent 675f0393e5
commit 94ea1ba33b
2 changed files with 68 additions and 29 deletions

View File

@ -271,37 +271,6 @@ namespace RMuseum.Services.Implementation
} }
} }
foreach (UploadSessionFile file in session.UploadedFiles.Where(file => Path.GetExtension(file.FilePath) == ".xml").ToList())
{
try
{
foreach (PoemAudio audio in PoemAudioListProcessor.Load(file.FilePath))
{
UploadSessionFile mp3file = mp3files.Where(mp3 => mp3.MP3FileCheckSum == audio.FileCheckSum).SingleOrDefault();
if(mp3file == null)
{
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResultMsg = "فایل mp3 متناظر یافت نشد (توجه فرمایید که همنامی اهمیت ندارد و فایل mp3 ارسالی باید دقیقاً همان فایلی باشد که همگامی با آن صورت گرفته است. اگر بعداً آن را جایگزین کرده‌اید مشخصات آن با مشخصات درج شده در فایل xml همسان نخواهد بود).";
context.UploadSessions.Update(session);
}
else
{
//here we should produce and save ogg file
byte[] mp3bytes = File.ReadAllBytes(mp3file.FilePath);
int mp3fileSize = mp3bytes.Length;
int oggfileSize;
using (MemoryStream ms = new MemoryStream(mp3bytes))
using (Mp3FileReader mp3FileReader = new Mp3FileReader(ms))
{
byte[] samples = new byte[mp3FileReader.Length];
await mp3FileReader.ReadAsync(samples, 0, (int)mp3FileReader.Length);
var oggBytes = ConvertRawPCMFile(mp3FileReader.Mp3WaveFormat.SampleRate, mp3FileReader.Mp3WaveFormat.Channels, samples, PCMSample.EightBit, mp3FileReader.Mp3WaveFormat.SampleRate, mp3FileReader.Mp3WaveFormat.Channels);
oggfileSize = oggBytes.Length;
File.WriteAllBytes(Path.Combine(Path.GetDirectoryName(mp3file.FilePath), $"{Path.GetFileNameWithoutExtension(mp3file.FilePath)}.ogg") , oggBytes);
}
UserNarrationProfile defProfile = await context.UserNarrationProfiles.Where(p => p.UserId == session.UseId && p.IsDefault == true).FirstOrDefaultAsync(); UserNarrationProfile defProfile = await context.UserNarrationProfiles.Where(p => p.UserId == session.UseId && p.IsDefault == true).FirstOrDefaultAsync();
if (defProfile == null) if (defProfile == null)
{ {
@ -318,24 +287,93 @@ namespace RMuseum.Services.Implementation
}; };
} }
//create draft poem narration:
string FileNameWithoutExtension = $"{audio.PoemId}-{defProfile.FileSuffixWithoutDash}"; foreach (UploadSessionFile file in session.UploadedFiles.Where(file => Path.GetExtension(file.FilePath) == ".xml").ToList())
{
try
{
//although each xml can theorically contain more than one file information
//this assumption was never implemented and used in Desktop Ganjoor which produces this xml file
//within the loop code the file is moved somewhere else and if the loop reaches is unexpected second path
//the code would fail!
foreach (PoemAudio audio in PoemAudioListProcessor.Load(file.FilePath))
{
string soundFilesFolder = Configuration.GetSection("AudioUploadService")["StoragePath"];
string targetPathForAudioFiles = Path.Combine(Configuration.GetSection("LocalAudioRepositoryPath")["StoragePath"], Configuration.GetSection("AudioUploadService")["StoragePath"]);
if (!Directory.Exists(targetPathForAudioFiles))
{
Directory.CreateDirectory(targetPathForAudioFiles);
}
string targetPathForXmlFiles = Path.Combine(targetPathForAudioFiles, "x");
if (!Directory.Exists(targetPathForXmlFiles))
{
Directory.CreateDirectory(targetPathForXmlFiles);
}
string fileNameWithoutExtension = $"{audio.PoemId}-{defProfile.FileSuffixWithoutDash}";
int tmp = 1;
while
(
File.Exists(Path.Combine(targetPathForAudioFiles, $"{fileNameWithoutExtension}.mp3"))
||
File.Exists(Path.Combine(targetPathForAudioFiles, $"{fileNameWithoutExtension}.ogg"))
||
File.Exists(Path.Combine(targetPathForXmlFiles, $"{fileNameWithoutExtension}.xml"))
)
{
fileNameWithoutExtension = $"{audio.PoemId}-{defProfile.FileSuffixWithoutDash}{tmp}";
tmp++;
}
string localXmlFilePath = Path.Combine(targetPathForXmlFiles, $"{fileNameWithoutExtension}.xml");
File.Move(file.FilePath, localXmlFilePath); //this is the movemnet I talked about earlier
string localMp3FilePath = Path.Combine(targetPathForAudioFiles, $"{fileNameWithoutExtension}.mp3");
UploadSessionFile mp3file = mp3files.Where(mp3 => mp3.MP3FileCheckSum == audio.FileCheckSum).SingleOrDefault();
if(mp3file == null)
{
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResultMsg = "فایل mp3 متناظر یافت نشد (توجه فرمایید که همنامی اهمیت ندارد و فایل mp3 ارسالی باید دقیقاً همان فایلی باشد که همگامی با آن صورت گرفته است. اگر بعداً آن را جایگزین کرده‌اید مشخصات آن با مشخصات درج شده در فایل xml همسان نخواهد بود).";
context.UploadSessions.Update(session);
}
else
{
File.Move(mp3file.FilePath, localMp3FilePath);
//here we should produce and save ogg file
byte[] mp3bytes = File.ReadAllBytes(Path.Combine(targetPathForAudioFiles, $"{fileNameWithoutExtension}.mp3"));
int mp3fileSize = mp3bytes.Length;
int oggfileSize;
using (MemoryStream ms = new MemoryStream(mp3bytes))
using (Mp3FileReader mp3FileReader = new Mp3FileReader(ms))
{
byte[] samples = new byte[mp3FileReader.Length];
await mp3FileReader.ReadAsync(samples, 0, (int)mp3FileReader.Length);
var oggBytes = ConvertRawPCMFile(mp3FileReader.Mp3WaveFormat.SampleRate, mp3FileReader.Mp3WaveFormat.Channels, samples, PCMSample.EightBit, mp3FileReader.Mp3WaveFormat.SampleRate, mp3FileReader.Mp3WaveFormat.Channels);
oggfileSize = oggBytes.Length;
File.WriteAllBytes(Path.Combine(targetPathForAudioFiles, $"{fileNameWithoutExtension}.ogg"), oggBytes);
}
Guid legacyAudioGuid = audio.SyncGuid; Guid legacyAudioGuid = audio.SyncGuid;
while( while(
(await context.AudioFiles.Where(a => a.LegacyAudioGuid == legacyAudioGuid).FirstOrDefaultAsync()) != null (await context.AudioFiles.Where(a => a.LegacyAudioGuid == legacyAudioGuid).FirstOrDefaultAsync()) != null
) )
{ legacyAudioGuid = Guid.NewGuid(); } {
legacyAudioGuid = Guid.NewGuid();
}
//TODO: rename and move files somewhere here
PoemNarration narration = new PoemNarration() PoemNarration narration = new PoemNarration()
{ {
OwnerId = session.UseId, OwnerId = session.UseId,
GanjoorAudioId = 1 + await context.AudioFiles.OrderByDescending(a => a.GanjoorAudioId).Select(a => a.GanjoorAudioId).FirstOrDefaultAsync(), GanjoorAudioId = 1 + await context.AudioFiles.OrderByDescending(a => a.GanjoorAudioId).Select(a => a.GanjoorAudioId).FirstOrDefaultAsync(),
AudioOrder = 1 + await context.AudioFiles.Where(a => a.GanjoorPostId == audio.PoemId).OrderByDescending(a => a.GanjoorAudioId).Select(a => a.GanjoorAudioId).FirstOrDefaultAsync(), AudioOrder = 1 + await context.AudioFiles.Where(a => a.GanjoorPostId == audio.PoemId).OrderByDescending(a => a.GanjoorAudioId).Select(a => a.GanjoorAudioId).FirstOrDefaultAsync(),
FileNameWithoutExtension = FileNameWithoutExtension, FileNameWithoutExtension = fileNameWithoutExtension,
SoundFilesFolder = "a2", SoundFilesFolder = soundFilesFolder,
AudioTitle = audio.PoemTitle, AudioTitle = audio.PoemTitle,
AudioArtist = defProfile.ArtistName, AudioArtist = defProfile.ArtistName,
AudioArtistUrl = defProfile.ArtistUrl, AudioArtistUrl = defProfile.ArtistUrl,
@ -346,8 +384,8 @@ namespace RMuseum.Services.Implementation
Mp3SizeInBytes = mp3fileSize, Mp3SizeInBytes = mp3fileSize,
OggSizeInBytes = oggfileSize, OggSizeInBytes = oggfileSize,
UploadDate = session.UploadEndTime, UploadDate = session.UploadEndTime,
LocalMp3FilePath = mp3file.FilePath, LocalMp3FilePath = localMp3FilePath,
LocalXmlFilePath = file.FilePath, LocalXmlFilePath = localXmlFilePath,
AudioSyncStatus = (int)AudioSyncStatus.NewItem, AudioSyncStatus = (int)AudioSyncStatus.NewItem,
ReviewStatus = AudioReviewStatus.Draft ReviewStatus = AudioReviewStatus.Draft
}; };

View File

@ -42,6 +42,7 @@
"AudioUploadService": { "AudioUploadService": {
"StoragePath": "C:\\audio", "StoragePath": "C:\\audio",
"LocalAudioRepositoryPath": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i", "LocalAudioRepositoryPath": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i",
"CurrentSoundFilesFolder": "a2"
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }