Refactored to use database
#5 Switched to use maria database to store song information #7 Added spring and hibernate support #8 Added song upload functionality
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: DiscordEventHandler.java
|
||||
*
|
||||
* Copyright 2019 Locusworks LLC.
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
@@ -43,6 +43,7 @@ import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||
import net.locusworks.discord.eighttrack.database.repos.GuildRepository;
|
||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
||||
import net.locusworks.discord.eighttrack.services.GuildMusicService;
|
||||
import net.locusworks.discord.eighttrack.services.GuildSongRepoService;
|
||||
import net.locusworks.logger.ApplicationLogger;
|
||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||
|
||||
@@ -64,6 +65,9 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
|
||||
@Autowired
|
||||
private Mp3UploadHandler uploadHandler;
|
||||
|
||||
@Autowired
|
||||
private GuildSongRepoService guildSongRepoService;
|
||||
|
||||
@PostConstruct
|
||||
private void init() throws IOException {
|
||||
@@ -106,7 +110,7 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
private void onGuildMessageReceivedHelper(GuildMessageReceivedEvent event) throws IOException {
|
||||
|
||||
if (!musicService.containsKey(event.getGuild().getIdLong())) {
|
||||
musicService.put(event.getGuild().getIdLong(), new GuildMusicHandler(musicDir, uploadHandler));
|
||||
musicService.put(event.getGuild().getIdLong(), new GuildMusicHandler(musicDir, uploadHandler, guildSongRepoService));
|
||||
}
|
||||
GuildMusicHandler gmh = musicService.get(event.getGuild().getIdLong());
|
||||
gmh.accept((id) -> musicService.remove(id));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: EightTrackAudioSendHandler.java
|
||||
*
|
||||
* Copyright 2019 Locusworks LLC.
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: GuildMusicHandler.java
|
||||
*
|
||||
* Copyright 2019 Locusworks LLC.
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
@@ -29,10 +29,13 @@ package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -51,7 +54,12 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.managers.AudioManager;
|
||||
import net.locusworks.crypto.utils.HashUtils;
|
||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||
import net.locusworks.discord.eighttrack.database.entities.Song;
|
||||
import net.locusworks.discord.eighttrack.scheduler.TrackScheduler;
|
||||
import net.locusworks.discord.eighttrack.services.GuildSongRepoService;
|
||||
import net.locusworks.logger.ApplicationLogger;
|
||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||
|
||||
@@ -69,13 +77,15 @@ public class GuildMusicHandler {
|
||||
|
||||
private Consumer<Long> callback;
|
||||
private Mp3UploadHandler uploadHandler;
|
||||
private GuildSongRepoService guildSongRepoService;
|
||||
|
||||
public GuildMusicHandler(Path musicDir, Mp3UploadHandler uploadHandler) throws IOException {
|
||||
public GuildMusicHandler(Path musicDir, Mp3UploadHandler uploadHandler, GuildSongRepoService guildSongRepoService) throws IOException {
|
||||
this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
|
||||
this.playing = new AtomicBoolean(false);
|
||||
this.musicDir = musicDir;
|
||||
this.lastPlayed = OffsetDateTime.now();
|
||||
this.uploadHandler = uploadHandler;
|
||||
this.guildSongRepoService = guildSongRepoService;
|
||||
this.apm = new DefaultAudioPlayerManager();
|
||||
|
||||
AudioSourceManagers.registerLocalSource(apm);
|
||||
@@ -211,16 +221,13 @@ public class GuildMusicHandler {
|
||||
|
||||
for(Attachment attachment : event.getMessage().getAttachments()) {
|
||||
attachment.retrieveInputStream().thenAccept((in) -> {
|
||||
Mp3UploadResults res = null;
|
||||
try {
|
||||
Mp3UploadResults res = uploadHandler.parse(in);
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setColor(Color.GREEN)
|
||||
.setTitle("Upload Results")
|
||||
.setDescription(res.toString())
|
||||
.setTimestamp(OffsetDateTime.now())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
MessageEmbed embed = null;
|
||||
res = uploadHandler.parse(in);
|
||||
if (res.validFile()) {
|
||||
embed =persistSong(res, event, attachment.getFileName());
|
||||
}
|
||||
|
||||
event.getChannel().sendMessage(embed).queue();
|
||||
} catch (Exception ex) {
|
||||
@@ -228,12 +235,72 @@ public class GuildMusicHandler {
|
||||
logger.error(ex);
|
||||
} finally {
|
||||
event.getMessage().delete().queue();
|
||||
if (res != null) res.clear();
|
||||
}
|
||||
|
||||
}).exceptionally((err) ->{
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private MessageEmbed persistSong(Mp3UploadResults result, GuildMessageReceivedEvent event, String fileName) throws Exception {
|
||||
|
||||
DiscordGuild guild = guildSongRepoService.getGuildRepo().findByGuildId(event.getGuild().getIdLong());
|
||||
if(guild == null) {
|
||||
throw new IOException("Unable to find guild in database. Please contact administrator");
|
||||
}
|
||||
|
||||
Path output = musicDir;
|
||||
if (result.getArtist() != null) {
|
||||
output = output.resolve(result.getArtist());
|
||||
if (result.getAlbum() != null) {
|
||||
output = output.resolve(result.getAlbum());
|
||||
}
|
||||
}
|
||||
output = output.resolve(fileName);
|
||||
|
||||
if (!Files.exists(output.toAbsolutePath().getParent())) {
|
||||
Files.createDirectories(output.toAbsolutePath().getParent());
|
||||
}
|
||||
|
||||
Files.write(output, result.getData());
|
||||
|
||||
Song song = new Song();
|
||||
song.setAlbum(result.getAlbum());
|
||||
song.setArtist(result.getArtist());
|
||||
song.setDateAdded(new Date());
|
||||
song.setDiscNumber(result.getDiscNumber());
|
||||
if (result.getDuration() != null)
|
||||
song.setDuration(BigInteger.valueOf(result.getDuration()));
|
||||
song.setFileHash(HashUtils.hash("SHA-1", result.getData()));
|
||||
song.setFilePath(output.toAbsolutePath().toString());
|
||||
song.setGenre(result.getGenre());
|
||||
song.setReleaseYear(result.getReleaseDate());
|
||||
song.setTitle(result.getTitle());
|
||||
song.setTrackNumber(result.getTrackNumber());
|
||||
song.setUuid(UUID.nameUUIDFromBytes(result.getData()).toString());
|
||||
|
||||
guildSongRepoService.getSongRepo().save(song);
|
||||
|
||||
GuildSong gs = new GuildSong();
|
||||
gs.setDateAdded(new Date());
|
||||
gs.setGuild(guild);
|
||||
gs.setSong(song);
|
||||
|
||||
guildSongRepoService.getGuildSongRepo().save(gs);
|
||||
|
||||
String out = String.format("```%s%n%-10s: %s```", result, "UUID", song.getUuid());
|
||||
|
||||
MessageEmbed embed = new EmbedBuilder()
|
||||
.setColor(Color.GREEN)
|
||||
.setTitle("Upload Results for " + fileName)
|
||||
.setDescription(out)
|
||||
.setTimestamp(OffsetDateTime.now())
|
||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||
.build();
|
||||
|
||||
return embed;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: Mp3UploadHandler.java
|
||||
*
|
||||
* Copyright 2019 Locusworks LLC.
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
@@ -27,6 +27,8 @@
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
@@ -50,16 +52,26 @@ public class Mp3UploadHandler {
|
||||
}
|
||||
|
||||
public Mp3UploadResults parse(InputStream is) throws IOException {
|
||||
try {
|
||||
byte[] data;
|
||||
try(InputStream in = is; ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||
int read = 0;
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
while((read = in.read(buffer)) > 0) {
|
||||
baos.write(buffer, 0, read);
|
||||
}
|
||||
data = baos.toByteArray();
|
||||
}
|
||||
|
||||
try (InputStream in = new ByteArrayInputStream(data)) {
|
||||
BodyContentHandler handler = new BodyContentHandler();
|
||||
Metadata metadata = new Metadata();
|
||||
ParseContext context = new ParseContext();
|
||||
|
||||
Mp3Parser parser = new Mp3Parser();
|
||||
|
||||
parser.parse(is, handler, metadata, context);
|
||||
parser.parse(in, handler, metadata, context);
|
||||
|
||||
return new Mp3UploadResults(metadata);
|
||||
return new Mp3UploadResults(metadata, data);
|
||||
} catch (Exception ex) {
|
||||
throw new IOException(ex);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: Mp3UploadResults.java
|
||||
*
|
||||
* Copyright 2019 Locusworks LLC.
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
@@ -63,9 +63,11 @@ public class Mp3UploadResults {
|
||||
private String releaseDate;
|
||||
private Long duration;
|
||||
|
||||
private byte[] rawData;
|
||||
|
||||
public Mp3UploadResults( Metadata metadata) {
|
||||
public Mp3UploadResults( Metadata metadata, byte[] data) {
|
||||
parseResults(metadata);
|
||||
this.rawData = data;
|
||||
}
|
||||
|
||||
private void parseResults(Metadata metadata) {
|
||||
@@ -83,6 +85,10 @@ public class Mp3UploadResults {
|
||||
duration = Double.valueOf(metadata.get(MetaDataField.DURATION.getValue())).longValue();
|
||||
}
|
||||
}
|
||||
|
||||
public final byte[] getData() {
|
||||
return this.rawData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the genre
|
||||
@@ -153,7 +159,7 @@ public class Mp3UploadResults {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("```");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (getTitle() != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Title", getTitle()));
|
||||
|
||||
@@ -178,10 +184,13 @@ public class Mp3UploadResults {
|
||||
if (duration != null)
|
||||
sb.append(String.format("%-10s: %s%n", "Duration", dateFormat(getDuration())));
|
||||
|
||||
sb.append("```");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
rawData = new byte[0];
|
||||
}
|
||||
|
||||
private String dateFormat(long timeInMilliSeconds) {
|
||||
long seconds = timeInMilliSeconds / 1000;
|
||||
long minutes = seconds / 60;
|
||||
|
||||
Reference in New Issue
Block a user