#93: Making some changes to how the file extension is determined
This commit is contained in:
@@ -2,6 +2,7 @@ namespace Icarus.Constants;
|
|||||||
|
|
||||||
public class FileExtensions
|
public class FileExtensions
|
||||||
{
|
{
|
||||||
|
public static string DEFAULT_AUDIO_EXTENSION = WAV_EXTENSION;
|
||||||
// Contains file extension with period at the beginning
|
// Contains file extension with period at the beginning
|
||||||
public static string MP3_EXTENSION = ".mp3";
|
public static string MP3_EXTENSION = ".mp3";
|
||||||
|
|
||||||
|
|||||||
@@ -308,28 +308,31 @@ public class SongManager : BaseManager
|
|||||||
Data = uncompressedSong
|
Data = uncompressedSong
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private async Task<Song> SaveSongTemp(IFormFile songFile)
|
public async Task<Song> SaveSongTemp(IFormFile songFile)
|
||||||
{
|
{
|
||||||
var song = new Song();
|
|
||||||
_logger.Info("Assigning song filename");
|
_logger.Info("Assigning song filename");
|
||||||
song.SongDirectory = _tempDirectoryRoot;
|
var song = new Song { SongDirectory = this._tempDirectoryRoot };
|
||||||
var filename = song.GenerateFilename(1);
|
var filename = await song.GenerateFilenameAsync(0) + "-" + songFile.FileName;
|
||||||
song.Filename = filename;
|
song.Filename = filename;
|
||||||
|
var songPath = song.SongPath();
|
||||||
|
|
||||||
using (var filestream = new FileStream(song.SongPath(), FileMode.Create))
|
using (var filestream = new FileStream(songPath, FileMode.Create))
|
||||||
{
|
{
|
||||||
_logger.Info("Saving temp song: {0}", song.SongPath());
|
_logger.Info("Saving temp song: {0}", songPath);
|
||||||
await songFile.CopyToAsync(filestream);
|
await songFile.CopyToAsync(filestream);
|
||||||
}
|
}
|
||||||
|
// For audio files that already contain embedded metadata
|
||||||
|
/*
|
||||||
await Task.Run(() =>
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
MetadataRetriever meta = new MetadataRetriever();
|
MetadataRetriever meta = new MetadataRetriever();
|
||||||
song = meta.RetrieveMetaData(song.SongPath());
|
song = meta.RetrieveMetaData(song.SongPath());
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
song.SongDirectory = _tempDirectoryRoot;
|
// song.SongDirectory = _tempDirectoryRoot;
|
||||||
song.DateCreated = DateTime.Now;
|
song.DateCreated = DateTime.Now;
|
||||||
song.Filename = filename;
|
// song.Filename = filename;
|
||||||
|
|
||||||
return song;
|
return song;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ public class MetadataRetriever
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream);
|
var fileType = FileTypeChecker.FileTypeValidator.GetFileType(fileStream);
|
||||||
Console.WriteLine($"Filetype: {fileType}");
|
Console.WriteLine($"Filetype: {fileType}");
|
||||||
Console.WriteLine($"Extension: {fileType.Extension}");
|
Console.WriteLine($"Extension: {fileType.Extension}");
|
||||||
@@ -108,6 +109,20 @@ public class MetadataRetriever
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string FileExtensionType(string path)
|
||||||
|
{
|
||||||
|
var extensionRaw = System.IO.Path.GetExtension(path);
|
||||||
|
|
||||||
|
if (extensionRaw[0] == '.')
|
||||||
|
{
|
||||||
|
return extensionRaw.Substring(1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return extensionRaw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool IsSupportedFile(IFormFile file)
|
public bool IsSupportedFile(IFormFile file)
|
||||||
{
|
{
|
||||||
var supportedTypes = this._supportedAudioFileTypes;
|
var supportedTypes = this._supportedAudioFileTypes;
|
||||||
@@ -124,6 +139,22 @@ public class MetadataRetriever
|
|||||||
return supportedTypes.Contains(extensionType);
|
return supportedTypes.Contains(extensionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsSupportedFile(string path)
|
||||||
|
{
|
||||||
|
var supportedTypes = this._supportedAudioFileTypes;
|
||||||
|
this._supportedImageFileTypes.ForEach(t =>
|
||||||
|
{
|
||||||
|
if (!supportedTypes.Contains(t))
|
||||||
|
{
|
||||||
|
supportedTypes.Add(t);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var extensionType = this.FileExtensionType(path).ToLower();
|
||||||
|
|
||||||
|
return supportedTypes.Contains(extensionType);
|
||||||
|
}
|
||||||
|
|
||||||
public Song RetrieveMetaData(string filePath)
|
public Song RetrieveMetaData(string filePath)
|
||||||
{
|
{
|
||||||
Song song = new Song();
|
Song song = new Song();
|
||||||
|
|||||||
@@ -104,11 +104,12 @@ public class SongDataController : BaseController
|
|||||||
if (up.SongData.Length > 0 && up.CoverArtData.Length > 0 && !string.IsNullOrEmpty(up.SongFile))
|
if (up.SongData.Length > 0 && up.CoverArtData.Length > 0 && !string.IsNullOrEmpty(up.SongFile))
|
||||||
{
|
{
|
||||||
var meta = new Utilities.MetadataRetriever();
|
var meta = new Utilities.MetadataRetriever();
|
||||||
if (!meta.IsSupportedFile(up.SongData) && !meta.IsSupportedFile(up.CoverArtData))
|
var tmpSong = this._songMgr.SaveSongTemp(up.SongData).Result;
|
||||||
|
if (!meta.IsSupportedFile(tmpSong.SongPath()) && !meta.IsSupportedFile(up.CoverArtData))
|
||||||
{
|
{
|
||||||
return BadRequest("Media is not supported");
|
return BadRequest("Media is not supported");
|
||||||
}
|
}
|
||||||
else if (!meta.IsSupportedFile(up.SongData))
|
else if (!meta.IsSupportedFile(tmpSong.SongPath()))
|
||||||
{
|
{
|
||||||
return BadRequest("Song is not supported");
|
return BadRequest("Song is not supported");
|
||||||
}
|
}
|
||||||
@@ -129,7 +130,7 @@ public class SongDataController : BaseController
|
|||||||
|
|
||||||
_logger.LogInformation($"Song title: {song.Title}");
|
_logger.LogInformation($"Song title: {song.Title}");
|
||||||
|
|
||||||
var fileType = meta.FileExtensionType(up.SongData);
|
var fileType = meta.FileExtensionType(tmpSong.SongPath());
|
||||||
|
|
||||||
switch (fileType)
|
switch (fileType)
|
||||||
{
|
{
|
||||||
|
|||||||
+2
-2
@@ -75,7 +75,7 @@ public class Song
|
|||||||
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
||||||
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
||||||
var filename = this.Generate(length, chars);
|
var filename = this.Generate(length, chars);
|
||||||
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
var extension = Icarus.Constants.FileExtensions.DEFAULT_AUDIO_EXTENSION;
|
||||||
|
|
||||||
return flag == 0 ? filename : $"{filename}{extension}";
|
return flag == 0 ? filename : $"{filename}{extension}";
|
||||||
}
|
}
|
||||||
@@ -83,7 +83,7 @@ public class Song
|
|||||||
{
|
{
|
||||||
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
int length = Constants.DirectoryPaths.FILENAME_LENGTH;
|
||||||
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
string chars = Constants.DirectoryPaths.FILENAME_CHARACTERS;
|
||||||
var extension = Icarus.Constants.FileExtensions.WAV_EXTENSION;
|
var extension = Icarus.Constants.FileExtensions.DEFAULT_AUDIO_EXTENSION;
|
||||||
var filename = await Task.Run(() =>
|
var filename = await Task.Run(() =>
|
||||||
{
|
{
|
||||||
return this.Generate(length, chars);
|
return this.Generate(length, chars);
|
||||||
|
|||||||
Reference in New Issue
Block a user