Merge pull request #10 from ganjoor/audioman
#7 processing uploaded narrations (the next job would be converting m…
This commit is contained in:
commit
433b011bb6
@ -95,8 +95,10 @@ namespace RMuseum.Controllers
|
||||
return BadRequest(rsFileResult.ExceptionString);
|
||||
}
|
||||
files.Add(rsFileResult.Result);
|
||||
}
|
||||
return Ok(await _audioService.GetUploadSession(resSession.Result.Id));
|
||||
}
|
||||
resSession = await _audioService.FinalizeNewUploadSession(resSession.Result, files.ToArray());
|
||||
|
||||
return Ok(resSession.Result);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
|
||||
@ -43,7 +43,11 @@ namespace RMuseum.Models.UploadSession
|
||||
/// </summary>
|
||||
public string FilePath { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// File check sum for mp3 files
|
||||
/// </summary>
|
||||
public string MP3FileCheckSum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Process Result
|
||||
/// </summary>
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\Implementation\ImportedFromDesktopGanjoor\" />
|
||||
<Folder Include="Swashbuckle\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -2809,6 +2809,11 @@
|
||||
File Path
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RMuseum.Models.UploadSession.UploadSessionFile.MP3FileCheckSum">
|
||||
<summary>
|
||||
File check sum for mp3 files
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RMuseum.Models.UploadSession.UploadSessionFile.ProcessResult">
|
||||
<summary>
|
||||
Process Result
|
||||
@ -3404,6 +3409,14 @@
|
||||
<param name="uploadedFile"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IAudioNarrationService.FinalizeNewUploadSession(RMuseum.Models.UploadSession.UploadSession,RMuseum.Models.UploadSession.UploadSessionFile[])">
|
||||
<summary>
|
||||
finalize upload session (add files)
|
||||
</summary>
|
||||
<param name="session"></param>
|
||||
<param name="files"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IAudioNarrationService.GetUploadSession(System.Guid)">
|
||||
<summary>
|
||||
Get Upload Session (including files)
|
||||
@ -4153,6 +4166,14 @@
|
||||
<param name="uploadedFile"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.FinalizeNewUploadSession(RMuseum.Models.UploadSession.UploadSession,RMuseum.Models.UploadSession.UploadSessionFile[])">
|
||||
<summary>
|
||||
finalize upload session (add files)
|
||||
</summary>
|
||||
<param name="session"></param>
|
||||
<param name="files"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.GetUploadSession(System.Guid)">
|
||||
<summary>
|
||||
Get Upload Session (including files)
|
||||
@ -4170,7 +4191,12 @@
|
||||
Database Contetxt
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration)">
|
||||
<member name="F:RMuseum.Services.Implementation.AudioNarrationService._backgroundTaskQueue">
|
||||
<summary>
|
||||
Background Task Queue Instance
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.#ctor(RMuseum.DbContext.RMuseumDbContext,Microsoft.Extensions.Configuration.IConfiguration,RMuseum.Services.IBackgroundTaskQueue)">
|
||||
<summary>
|
||||
constructor
|
||||
</summary>
|
||||
@ -4432,5 +4458,137 @@
|
||||
<param name="operation"></param>
|
||||
<param name="context"></param>
|
||||
</member>
|
||||
<member name="T:ganjoor.PoemAudio">
|
||||
<summary>
|
||||
اطلاعات فایل صوتی شعر
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudio.#ctor">
|
||||
<summary>
|
||||
سازنده
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.PoemId">
|
||||
<summary>
|
||||
شناسۀ شعر
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.Id">
|
||||
<summary>
|
||||
شناسۀ فایل صوتی با شروع از 1 برای هر شعر
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.FilePath">
|
||||
<summary>
|
||||
مسیر فایل
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.Description">
|
||||
<summary>
|
||||
شرح
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.IsSynced">
|
||||
<summary>
|
||||
آیا همگام شده
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:ganjoor.PoemAudio.SyncInfo">
|
||||
<summary>
|
||||
ساختار اطلاعات همگام سازی
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:ganjoor.PoemAudio.SyncInfo.VerseOrder">
|
||||
<summary>
|
||||
ترتیب مصرع (-1 نشانگر آغاز فایل صوتی و -2 نشانگر پایان آن است)
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:ganjoor.PoemAudio.SyncInfo.AudioMiliseconds">
|
||||
<summary>
|
||||
زمان در فایل صوتی بر حسب میلی ثانیه
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.SyncArray">
|
||||
<summary>
|
||||
اطلاعات همگام سازی
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.DownloadUrl">
|
||||
<summary>
|
||||
نشانی دریافت فایل صوتی
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.IsDirectlyDownloadable">
|
||||
<summary>
|
||||
آیا لینک دریافت فایل صوتی مستقیم است
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.SyncGuid">
|
||||
<summary>
|
||||
شناسۀ یکتای اطلاعات همگام سازی
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.FileCheckSum">
|
||||
<summary>
|
||||
امضای یکتای فایل صوتی
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudio.ComputeCheckSum(System.String)">
|
||||
<summary>
|
||||
محاسبه چک سام فایل
|
||||
</summary>
|
||||
<param name="filePath"></param>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.IsUploaded">
|
||||
<summary>
|
||||
آیا اطلاعات همگام سازی آپلود شده؟
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudio.ToString">
|
||||
<summary>
|
||||
تبدیل به رشته
|
||||
</summary>
|
||||
<returns>شرح</returns>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.PoetName">
|
||||
<remarks>
|
||||
Warning: not always filled
|
||||
</remarks>
|
||||
</member>
|
||||
<member name="P:ganjoor.PoemAudio.PoemTitle">
|
||||
<remarks>
|
||||
Warning: Rarely filled
|
||||
</remarks>
|
||||
</member>
|
||||
<member name="T:ganjoor.PoemAudioListProcessor">
|
||||
<summary>
|
||||
پردازشگر ذخیره و بارگذاری لیستهای اشعار
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudioListProcessor.Save(System.String,ganjoor.PoemAudio,System.Boolean)">
|
||||
<summary>
|
||||
ذخیره در فایل
|
||||
</summary>
|
||||
<param name="FileName"></param>
|
||||
<param name="poemAudio"></param>
|
||||
<param name="oldVersion"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudioListProcessor.Save(System.String,System.Collections.Generic.List{ganjoor.PoemAudio},System.Boolean)">
|
||||
<summary>
|
||||
ذخیره در فایل
|
||||
</summary>
|
||||
<param name="FileName"></param>
|
||||
<param name="List"></param>
|
||||
<param name="oldVersion"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:ganjoor.PoemAudioListProcessor.Load(System.String)">
|
||||
<summary>
|
||||
بارگذاری از فایل
|
||||
</summary>
|
||||
<param name="FileName"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
|
||||
@ -43,11 +43,20 @@ namespace RMuseum.Services
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<UploadSessionFile>> SaveUploadedFile(IFormFile uploadedFile);
|
||||
|
||||
/// <summary>
|
||||
/// finalize upload session (add files)
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<UploadSession>> FinalizeNewUploadSession(UploadSession session, UploadSessionFile[] files);
|
||||
|
||||
/// <summary>
|
||||
/// Get Upload Session (including files)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<UploadSession>> GetUploadSession(Guid id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Audit.WebApi;
|
||||
using ganjoor;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MySql.Data.MySqlClient;
|
||||
@ -183,14 +185,13 @@ namespace RMuseum.Services.Implementation
|
||||
FileName = uploadedFile.FileName,
|
||||
Length = uploadedFile.Length,
|
||||
Name = uploadedFile.Name,
|
||||
ProcessResult = true,
|
||||
ProcessResultMsg = ""
|
||||
ProcessResult = false,
|
||||
ProcessResultMsg = "پردازش نشده (فایلهای mp3 که مشخصات آنها در فایلهای xml ارسالی یافت نشود پردازش نمیشوند)."
|
||||
};
|
||||
|
||||
string ext = Path.GetExtension(file.FileName).ToLower();
|
||||
if(ext != ".mp3" && ext != ".xml" && ext != ".zip")
|
||||
{
|
||||
file.ProcessResult = false;
|
||||
file.ProcessResultMsg = "تنها فایلهای با پسوند mp3، xml و zip قابل قبول هستند.";
|
||||
}
|
||||
else
|
||||
@ -228,6 +229,91 @@ namespace RMuseum.Services.Implementation
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// finalize upload session (add files)
|
||||
/// </summary>
|
||||
/// <param name="session"></param>
|
||||
/// <param name="files"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<UploadSession>> FinalizeNewUploadSession(UploadSession session, UploadSessionFile[] files)
|
||||
{
|
||||
try
|
||||
{
|
||||
session.UploadedFiles = files;
|
||||
|
||||
_context.UploadSessions.Update(session);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_backgroundTaskQueue.QueueBackgroundWorkItem
|
||||
(
|
||||
async token =>
|
||||
{
|
||||
using (RMuseumDbContext context = new RMuseumDbContext(Configuration)) //this is long running job, so _context might be already been freed/collected by GC
|
||||
{
|
||||
List<UploadSessionFile> mp3files = new List<UploadSessionFile>();
|
||||
foreach (UploadSessionFile file in session.UploadedFiles.Where(file => Path.GetExtension(file.FilePath) == ".mp3").ToList())
|
||||
{
|
||||
try
|
||||
{
|
||||
file.MP3FileCheckSum = PoemAudio.ComputeCheckSum(file.FilePath);
|
||||
mp3files.Add(file);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResultMsg = exp.ToString();
|
||||
context.UploadSessions.Update(session);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResultMsg = "";
|
||||
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResult = true;
|
||||
session.UploadedFiles.Where(f => f.Id == mp3file.Id).SingleOrDefault().ProcessResultMsg = "";
|
||||
session.UploadedFiles.Where(f => f.Id == mp3file.Id).SingleOrDefault().ProcessResult = true;
|
||||
}
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
session.UploadedFiles.Where(f => f.Id == file.Id).SingleOrDefault().ProcessResultMsg = "فایل XML نامعتبر است. اطلاعات بیشتر: " + exp.ToString();
|
||||
context.UploadSessions.Update(session);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
return new RServiceResult<UploadSession>(session);
|
||||
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<UploadSession>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get Upload Session (including files)
|
||||
/// </summary>
|
||||
@ -260,15 +346,21 @@ namespace RMuseum.Services.Implementation
|
||||
/// </summary>
|
||||
protected readonly RMuseumDbContext _context;
|
||||
|
||||
/// <summary>
|
||||
/// Background Task Queue Instance
|
||||
/// </summary>
|
||||
protected readonly IBackgroundTaskQueue _backgroundTaskQueue;
|
||||
|
||||
/// <summary>
|
||||
/// constructor
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public AudioNarrationService(RMuseumDbContext context, IConfiguration configuration)
|
||||
public AudioNarrationService(RMuseumDbContext context, IConfiguration configuration, IBackgroundTaskQueue backgroundTaskQueue)
|
||||
{
|
||||
_context = context;
|
||||
Configuration = configuration;
|
||||
_backgroundTaskQueue = backgroundTaskQueue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
|
||||
namespace ganjoor
|
||||
{
|
||||
/// <summary>
|
||||
/// اطلاعات فایل صوتی شعر
|
||||
/// </summary>
|
||||
public class PoemAudio
|
||||
{
|
||||
/// <summary>
|
||||
/// سازنده
|
||||
/// </summary>
|
||||
public PoemAudio()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// شناسۀ شعر
|
||||
/// </summary>
|
||||
public int PoemId
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// شناسۀ فایل صوتی با شروع از 1 برای هر شعر
|
||||
/// </summary>
|
||||
public int Id
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// مسیر فایل
|
||||
/// </summary>
|
||||
public string FilePath
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// شرح
|
||||
/// </summary>
|
||||
public string Description
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// آیا همگام شده
|
||||
/// </summary>
|
||||
public bool IsSynced
|
||||
{
|
||||
get
|
||||
{
|
||||
return SyncArray != null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// ساختار اطلاعات همگام سازی
|
||||
/// </summary>
|
||||
public struct SyncInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// ترتیب مصرع (-1 نشانگر آغاز فایل صوتی و -2 نشانگر پایان آن است)
|
||||
/// </summary>
|
||||
public int VerseOrder;
|
||||
/// <summary>
|
||||
/// زمان در فایل صوتی بر حسب میلی ثانیه
|
||||
/// </summary>
|
||||
public int AudioMiliseconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// اطلاعات همگام سازی
|
||||
/// </summary>
|
||||
public SyncInfo[] SyncArray
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// نشانی دریافت فایل صوتی
|
||||
/// </summary>
|
||||
public string DownloadUrl
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// آیا لینک دریافت فایل صوتی مستقیم است
|
||||
/// </summary>
|
||||
public bool IsDirectlyDownloadable
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// شناسۀ یکتای اطلاعات همگام سازی
|
||||
/// </summary>
|
||||
public Guid SyncGuid
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// امضای یکتای فایل صوتی
|
||||
/// </summary>
|
||||
public string FileCheckSum
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// محاسبه چک سام فایل
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
public static string ComputeCheckSum(string filePath)
|
||||
{
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var stream = File.OpenRead(filePath))
|
||||
{
|
||||
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ""; //problem with file
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// آیا اطلاعات همگام سازی آپلود شده؟
|
||||
/// </summary>
|
||||
public bool IsUploaded
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// تبدیل به رشته
|
||||
/// </summary>
|
||||
/// <returns>شرح</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return this.Description;
|
||||
}
|
||||
|
||||
|
||||
///<remarks>
|
||||
///Warning: not always filled
|
||||
///</remarks>
|
||||
public string PoetName
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// Warning: Rarely filled
|
||||
/// </remarks>
|
||||
public string PoemTitle
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
|
||||
namespace ganjoor
|
||||
{
|
||||
/// <summary>
|
||||
/// پردازشگر ذخیره و بارگذاری لیستهای اشعار
|
||||
/// </summary>
|
||||
public class PoemAudioListProcessor
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// ذخیره در فایل
|
||||
/// </summary>
|
||||
/// <param name="FileName"></param>
|
||||
/// <param name="poemAudio"></param>
|
||||
/// <param name="oldVersion"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Save(string FileName, PoemAudio poemAudio, bool oldVersion)
|
||||
{
|
||||
List<PoemAudio> lst = new List<PoemAudio>();
|
||||
lst.Add(poemAudio);
|
||||
return Save(FileName, lst, oldVersion);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ذخیره در فایل
|
||||
/// </summary>
|
||||
/// <param name="FileName"></param>
|
||||
/// <param name="List"></param>
|
||||
/// <param name="oldVersion"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Save(string FileName, List<PoemAudio> List, bool oldVersion)
|
||||
{
|
||||
XmlDocument doc = new XmlDocument();
|
||||
XmlNode poemAudioRootNode = doc.CreateNode(XmlNodeType.Element, "DesktopGanjoorPoemAudioList", "");
|
||||
doc.AppendChild(poemAudioRootNode);
|
||||
foreach (PoemAudio audio in List)
|
||||
{
|
||||
XmlNode poemAudioNode = doc.CreateNode(XmlNodeType.Element, "PoemAudio", "");
|
||||
foreach (PropertyInfo prop in typeof(PoemAudio).GetProperties())
|
||||
{
|
||||
if (!prop.CanWrite)
|
||||
continue;
|
||||
bool ignoreProp = false;
|
||||
XmlNode propNode = doc.CreateNode(XmlNodeType.Element, prop.Name, "");
|
||||
if (prop.PropertyType == typeof(string))
|
||||
{
|
||||
string value = prop.GetValue(audio, null) == null ? "" : prop.GetValue(audio, null).ToString();
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
ignoreProp = true;
|
||||
}
|
||||
else
|
||||
propNode.InnerText = value;
|
||||
}
|
||||
else
|
||||
if (prop.PropertyType == typeof(Int32))
|
||||
{
|
||||
int value = Convert.ToInt32(prop.GetValue(audio, null));
|
||||
if (value == 0)
|
||||
{
|
||||
ignoreProp = true;
|
||||
}
|
||||
else
|
||||
propNode.InnerText = value.ToString();
|
||||
}
|
||||
else
|
||||
if (prop.PropertyType == typeof(bool))
|
||||
{
|
||||
bool value = Convert.ToBoolean(prop.GetValue(audio, null));
|
||||
if (!value)
|
||||
{
|
||||
ignoreProp = true;
|
||||
}
|
||||
else
|
||||
propNode.InnerText = value.ToString();
|
||||
}
|
||||
else
|
||||
if (prop.PropertyType == typeof(Guid))
|
||||
{
|
||||
string value = prop.GetValue(audio, null).ToString();
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
ignoreProp = true;
|
||||
}
|
||||
else
|
||||
propNode.InnerText = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
ignoreProp = true;
|
||||
}
|
||||
|
||||
if (!ignoreProp)
|
||||
poemAudioNode.AppendChild(propNode);
|
||||
}
|
||||
//رفع اشکال نسخه قدیمی NAudio
|
||||
XmlNode bugfixNode = doc.CreateNode(XmlNodeType.Element, "OneSecondBugFix", "");
|
||||
bugfixNode.InnerText = "1000";
|
||||
poemAudioNode.AppendChild(bugfixNode);
|
||||
|
||||
XmlNode syncInfoArrayNode = doc.CreateNode(XmlNodeType.Element, "SyncArray", "");
|
||||
if (audio.SyncArray != null)
|
||||
{
|
||||
|
||||
foreach (PoemAudio.SyncInfo info in audio.SyncArray)
|
||||
{
|
||||
XmlNode syncInfoNode = doc.CreateNode(XmlNodeType.Element, "SyncInfo", "");
|
||||
|
||||
XmlNode vOrderNode = doc.CreateNode(XmlNodeType.Element, "VerseOrder", "");
|
||||
vOrderNode.InnerText = info.VerseOrder.ToString();
|
||||
syncInfoNode.AppendChild(vOrderNode);
|
||||
|
||||
XmlNode vAudioMiliseconds = doc.CreateNode(XmlNodeType.Element, "AudioMiliseconds", "");
|
||||
if (oldVersion)
|
||||
{
|
||||
vAudioMiliseconds.InnerText = (info.AudioMiliseconds/2).ToString();
|
||||
}
|
||||
else
|
||||
vAudioMiliseconds.InnerText = info.AudioMiliseconds.ToString();
|
||||
syncInfoNode.AppendChild(vAudioMiliseconds);
|
||||
|
||||
|
||||
syncInfoArrayNode.AppendChild(syncInfoNode);
|
||||
}
|
||||
}
|
||||
|
||||
poemAudioNode.AppendChild(syncInfoArrayNode);
|
||||
poemAudioRootNode.AppendChild(poemAudioNode);
|
||||
}
|
||||
try
|
||||
{
|
||||
doc.Save(FileName);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// بارگذاری از فایل
|
||||
/// </summary>
|
||||
/// <param name="FileName"></param>
|
||||
/// <returns></returns>
|
||||
public static List<PoemAudio> Load(string FileName)
|
||||
{
|
||||
List<PoemAudio> lstPoemAudio = new List<PoemAudio>();
|
||||
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.LoadXml(File.ReadAllText(FileName));
|
||||
|
||||
//Collect List:
|
||||
XmlNodeList poemAudioNodes = doc.GetElementsByTagName("PoemAudio");
|
||||
foreach (XmlNode poemAudioNode in poemAudioNodes)
|
||||
{
|
||||
PoemAudio poemAudioInfo = new PoemAudio();
|
||||
poemAudioInfo.IsDirectlyDownloadable = false;
|
||||
poemAudioInfo.IsUploaded = false;
|
||||
foreach (XmlNode Node in poemAudioNode.ChildNodes)
|
||||
{
|
||||
switch (Node.Name)
|
||||
{
|
||||
case "PoemId":
|
||||
poemAudioInfo.PoemId = Convert.ToInt32(Node.InnerText);
|
||||
break;
|
||||
case "Id":
|
||||
poemAudioInfo.Id = Convert.ToInt32(Node.InnerText);
|
||||
break;
|
||||
case "DownloadUrl":
|
||||
poemAudioInfo.DownloadUrl = Node.InnerText;
|
||||
break;
|
||||
case "FilePath":
|
||||
poemAudioInfo.FilePath = Node.InnerText;
|
||||
break;
|
||||
case "Description":
|
||||
poemAudioInfo.Description = Node.InnerText;
|
||||
break;
|
||||
case "SyncGuid":
|
||||
poemAudioInfo.SyncGuid = Guid.Parse(Node.InnerText);
|
||||
break;
|
||||
case "FileCheckSum":
|
||||
poemAudioInfo.FileCheckSum = Node.InnerText;
|
||||
break;
|
||||
case "IsDirectlyDownloadable":
|
||||
poemAudioInfo.IsDirectlyDownloadable = Convert.ToBoolean(Node.InnerText);
|
||||
break;
|
||||
case "IsUploaded":
|
||||
poemAudioInfo.IsDirectlyDownloadable = Convert.ToBoolean(Node.InnerText);
|
||||
break;
|
||||
case "SyncArray":
|
||||
{
|
||||
XmlNodeList syncInfoNodeList = Node.SelectNodes("SyncInfo");
|
||||
if (syncInfoNodeList != null)
|
||||
{
|
||||
List<PoemAudio.SyncInfo> lstSyncInfo = new List<PoemAudio.SyncInfo>();
|
||||
|
||||
foreach (XmlNode nodeSyncIndo in syncInfoNodeList)
|
||||
{
|
||||
|
||||
lstSyncInfo.Add(
|
||||
new PoemAudio.SyncInfo()
|
||||
{
|
||||
VerseOrder = Convert.ToInt32(nodeSyncIndo.SelectSingleNode("VerseOrder").InnerText),
|
||||
AudioMiliseconds = Convert.ToInt32(nodeSyncIndo.SelectSingleNode("AudioMiliseconds").InnerText),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (lstSyncInfo.Count > 0)
|
||||
{
|
||||
poemAudioInfo.SyncArray = lstSyncInfo.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
lstPoemAudio.Add(poemAudioInfo);
|
||||
}
|
||||
|
||||
|
||||
return lstPoemAudio;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user