Commit e1dbdf99 authored by Kevin Whitaker's avatar Kevin Whitaker

Move extension checking into add method. Change scan results to not have...

Move extension checking into add method. Change scan results to not have booleans and cause issues with switches. Implement moving/checking/adding uploaded track to library.
parent 4233e5fb
......@@ -577,6 +577,16 @@ AudioTrack GroovePlayerMgr::getCurrentTrackDB(Wt::Dbo::Session* session)
GroovePlayerMgr::ScanResults GroovePlayerMgr::addFileToTrackDBIfTagged(Wt::Dbo::Session* session, std::filesystem::path file)
{
//Make sure file matches file type
std::string extensionLowered;
for(auto elem : file.extension().string())
{
extensionLowered.push_back(std::tolower(elem));
}
if(extensionLowered != ".mp3")
{
return ScanResults::WRONG_FILETYPE;
}
//Now check if tags exist and put into DB.
struct GrooveFile* gfile = groove_file_open(file.c_str());
struct GrooveTag* artist_tag = groove_file_metadata_get(gfile, "artist", nullptr, 0);
......@@ -706,6 +716,18 @@ void GroovePlayerMgr::getPictureFromTrack(AudioTrack* trackToFill)
}
}
bool GroovePlayerMgr::fileExistsInLibrary(std::string filename)
{
for(std::filesystem::directory_entry p: std::filesystem::directory_iterator(musicScanDir))
{
if(p.path().filename() == filename)
{
return true;
}
}
return false;
}
void GroovePlayerMgr::grooveAudioScannerLoop()
{
......@@ -724,21 +746,13 @@ void GroovePlayerMgr::grooveAudioScannerLoop()
}
for(std::filesystem::directory_entry p: std::filesystem::directory_iterator(musicScanDir))
{
std::string extensionLowered;
for(auto elem : p.path().extension().string())
if(addFileToTrackDBIfTagged(&sqlSession, p.path()) == ScanResults::ACCEPTED)
{
extensionLowered.push_back(std::tolower(elem));
}
if(extensionLowered == ".mp3") //TODO:think about supporting more than mp3s.
{
if(addFileToTrackDBIfTagged(&sqlSession, p.path()))
{
Wt::log("info") << p.path().string() << " was added to DB";
}
Wt::log("info") << p.path().string() << " was added to DB";
}
else
{
Wt::log("info") << p.path().string() + " was not an accepted audio file.";
Wt::log("info") << p.path().string() + " was not accepted.";
}
}
//Now check for tracks in DB without a file.
......
......@@ -56,7 +56,7 @@ public:
std::list<User> currentSkipRequester;
std::chrono::steady_clock::time_point skipRequestedAt;
enum ScanResults {ACCEPTED = true, MISSING_ARTIST_TAG = false, MISSING_ALBUM_TAG = false, MISSING_TITLE_TAG = false, MISSING_GENRE_TAG = false, DUPLICATE_TRACK = false, MISSING_COVERART_TAG = false, MISSING_MULTIPLE_TAGS = false};
enum ScanResults {ACCEPTED, MISSING_ARTIST_TAG, MISSING_ALBUM_TAG, MISSING_TITLE_TAG, MISSING_GENRE_TAG, DUPLICATE_TRACK, MISSING_COVERART_TAG, MISSING_MULTIPLE_TAGS, WRONG_FILETYPE};
ScanResults addFileToTrackDBIfTagged(Wt::Dbo::Session* session, std::filesystem::path file);
enum PlayerEventType {NOTHING, GROOVE_NOWPLAYING, VOTING_ENDED, VOTE_CAST, PLAYING_PAUSED, PLAYING_RESUMED, SKIP_REQUESTED, SKIP_VOTE_CAST_AGAINST, SKIP_VOTING_ENDED, ADMIN_FORCE_SKIP};
......@@ -72,6 +72,7 @@ public:
void getPictureFromTrack(AudioTrack* trackToFill);
void removeTrack(Wt::Dbo::Session* session, AudioTrack track);
bool fileExistsInLibrary(std::string filename);
private:
GroovePlayerMgr (std::string dbFile);
~GroovePlayerMgr();
......
......@@ -18,8 +18,119 @@
*/
#include "UploadInterface.h"
#include "../db/UserAction.h"
#include <experimental/filesystem> //TODO:Change to non-gcc way when officially using c++17
namespace std {
namespace filesystem = experimental::filesystem;
}
UploadInterface::UploadInterface(WebInterface* app)
{
this->app = app;
mainLayout = new Wt::WVBoxLayout();
fileDrop = new Wt::WFileUpload();
logs = new Wt::WTextArea();
clearLogs = new Wt::WPushButton("clear");
logs->setReadOnly(true);
reInitUploadBox();
this->setLayout(mainLayout);
mainLayout->addWidget(clearLogs);
mainLayout->addWidget(logs);
}
void UploadInterface::reInitUploadBox()
{
if(fileDrop != nullptr) delete fileDrop;
fileDrop = new Wt::WFileUpload();
mainLayout->insertWidget(0,fileDrop);
fileDrop->setProgressBar(new Wt::WProgressBar());
fileDrop->setFilters("audio/mpeg audio/mp3");
fileDrop->setMultiple(false);
fileDrop->changed().connect(this, &UploadInterface::fileDropChanged);
fileDrop->uploaded().connect(this, &UploadInterface::fileDropUploaded);
fileDrop->fileTooLarge().connect(this, &UploadInterface::fileDropTooLarge);
}
void UploadInterface::fileDropChanged()
{
if(fileDrop->canUpload()) fileDrop->upload();
}
void UploadInterface::fileDropTooLarge()
{
logs->setText(logs->text()+fileDrop->clientFileName()+": File too large\n");
reInitUploadBox();
}
void UploadInterface::fileDropUploaded()
{
//Make sure filename doesn't exist already and then move to music folder.
if(GroovePlayerMgr::getInstance()->fileExistsInLibrary(fileDrop->clientFileName().toUTF8()))
{
logs->setText(logs->text()+fileDrop->clientFileName()+" file not uploaded, conflicts with existing file.\n");
reInitUploadBox();
return;
}
std::filesystem::path libFile = GroovePlayerMgr::getInstance()->musicScanDir/fileDrop->clientFileName().toUTF8();
std::filesystem::copy_file(std::filesystem::path(fileDrop->spoolFileName()),libFile);
//Add to library and start new upload box
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
GroovePlayerMgr::ScanResults result = GroovePlayerMgr::getInstance()->addFileToTrackDBIfTagged(&sqlSession,libFile.string());
if(result != GroovePlayerMgr::ScanResults::ACCEPTED)
{
//Remove file
remove(libFile.c_str());
}
switch(result)
{
case GroovePlayerMgr::ScanResults::WRONG_FILETYPE:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is not an accepted filetype.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_TITLE_TAG:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing title tag.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_MULTIPLE_TAGS:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing multiple tags.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_GENRE_TAG:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing genre tag.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_COVERART_TAG:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing cover art.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_ARTIST_TAG:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing artist tag.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::MISSING_ALBUM_TAG:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is missing album tag..\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::DUPLICATE_TRACK:
logs->setText(logs->text()+fileDrop->clientFileName()+" file is a duplicate.\n");
reInitUploadBox();
break;
case GroovePlayerMgr::ScanResults::ACCEPTED:
logs->setText(logs->text()+fileDrop->clientFileName()+" file uploaded\n");
reInitUploadBox();
break;
}
}
void UploadInterface::clearLogsClicked()
{
logs->setText("");
}
......@@ -22,12 +22,29 @@
#include <Wt/WContainerWidget>
#include "../WebInterface.h"
#include <Wt/WVBoxLayout>
#include <Wt/WPushButton>
#include <Wt/WTextArea>
#include <Wt/WFileUpload>
#include <Wt/WProgressBar>
class UploadInterface : public Wt::WContainerWidget
{
public:
UploadInterface(WebInterface* app);
WebInterface* app;
Wt::WVBoxLayout* mainLayout;
Wt::WFileUpload* fileDrop;
Wt::WTextArea* logs;
Wt::WPushButton* clearLogs;
void reInitUploadBox();
void fileDropChanged();
void fileDropTooLarge();
void fileDropUploaded();
void clearLogsClicked();
};
#endif // UPLOADINTERFACE_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment