From 89add83bcd977c785e7b0e5e2ab29cf8c93370ef Mon Sep 17 00:00:00 2001 From: Dank074 Date: Thu, 8 Aug 2019 16:22:35 -0500 Subject: [PATCH] Added ability to upload image using ftp --- src/main/java/org/krews/apollyon/Main.java | 4 +++ .../krews/apollyon/ftp/FTPUploadService.java | 26 +++++++++++++++++++ .../incoming/CameraRoomPictureEvent.java | 14 ++++++++-- .../incoming/CameraRoomThumbnailEvent.java | 11 +++++++- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/krews/apollyon/ftp/FTPUploadService.java diff --git a/src/main/java/org/krews/apollyon/Main.java b/src/main/java/org/krews/apollyon/Main.java index 05514e9..5854799 100644 --- a/src/main/java/org/krews/apollyon/Main.java +++ b/src/main/java/org/krews/apollyon/Main.java @@ -57,6 +57,10 @@ public class Main extends HabboPlugin implements EventListener { public void onEmulatorLoadedEvent(EmulatorLoadedEvent e) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, Exception { // Adds missing sqls if they are not found. + Emulator.getConfig().register("ftp.enabled", "0"); + Emulator.getConfig().register("ftp.user", "root"); + Emulator.getConfig().register("ftp.password", "password123"); + Emulator.getConfig().register("ftp.host", "example.com"); Emulator.getConfig().register("camera.url", "http://yourdomain.com/swfdirectory/camera/"); Emulator.getConfig().register("imager.location.output.camera", "C:\\yourdirectory\\swfdirectory\\camera\\"); Emulator.getConfig().register("imager.location.output.thumbnail", "C:\\yourdirectory\\swfdirectory\\camera\\thumbnails\\thumbnail_"); diff --git a/src/main/java/org/krews/apollyon/ftp/FTPUploadService.java b/src/main/java/org/krews/apollyon/ftp/FTPUploadService.java new file mode 100644 index 0000000..6e9950c --- /dev/null +++ b/src/main/java/org/krews/apollyon/ftp/FTPUploadService.java @@ -0,0 +1,26 @@ +package org.krews.apollyon.ftp; + +import com.eu.habbo.Emulator; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; + +public class FTPUploadService { + private static final String ftpUrl = "ftp://%s:%s@%s/%s;type=i"; + + public static void uploadImage(byte[] image, String uploadPath) throws IOException{ + String host = Emulator.getConfig().getValue("ftp.host"); + String user = Emulator.getConfig().getValue("ftp.user"); + String pass = Emulator.getConfig().getValue("ftp.password"); + + String uploadURL = String.format(ftpUrl, user, pass, host, uploadPath); + + URL url = new URL(uploadURL); + URLConnection conn = url.openConnection(); + OutputStream outputStream = conn.getOutputStream(); + outputStream.write(image, 0, image.length); + outputStream.close(); + } +} diff --git a/src/main/java/org/krews/apollyon/incoming/CameraRoomPictureEvent.java b/src/main/java/org/krews/apollyon/incoming/CameraRoomPictureEvent.java index e92db62..7f35ffd 100644 --- a/src/main/java/org/krews/apollyon/incoming/CameraRoomPictureEvent.java +++ b/src/main/java/org/krews/apollyon/incoming/CameraRoomPictureEvent.java @@ -8,8 +8,11 @@ import com.eu.habbo.messages.outgoing.camera.CameraURLComposer; import com.eu.habbo.messages.outgoing.generic.alerts.GenericAlertComposer; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; +import org.krews.apollyon.ftp.FTPUploadService; + import javax.imageio.ImageIO; import java.awt.image.BufferedImage; +import java.awt.image.DataBufferByte; import java.io.File; import java.io.IOException; import java.lang.IllegalArgumentException; @@ -53,8 +56,15 @@ public class CameraRoomPictureEvent extends MessageHandler try { BufferedImage theImage = ImageIO.read(new ByteBufInputStream(image)); - ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.camera") + URL)); - ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.camera") + URL_small)); + if(Emulator.getConfig().getInt("ftp.enabled") == 1) { + byte[] imageBytes = ((DataBufferByte) theImage.getData().getDataBuffer()).getData(); + FTPUploadService.uploadImage(imageBytes, Emulator.getConfig().getValue("imager.location.output.camera") + URL); + FTPUploadService.uploadImage(imageBytes, Emulator.getConfig().getValue("imager.location.output.camera") + URL_small); + } + else { + ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.camera") + URL)); + ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.camera") + URL_small)); + } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { diff --git a/src/main/java/org/krews/apollyon/incoming/CameraRoomThumbnailEvent.java b/src/main/java/org/krews/apollyon/incoming/CameraRoomThumbnailEvent.java index 381c9a9..5bcda85 100644 --- a/src/main/java/org/krews/apollyon/incoming/CameraRoomThumbnailEvent.java +++ b/src/main/java/org/krews/apollyon/incoming/CameraRoomThumbnailEvent.java @@ -7,8 +7,11 @@ import com.eu.habbo.messages.outgoing.camera.CameraRoomThumbnailSavedComposer; import com.eu.habbo.messages.outgoing.generic.alerts.GenericAlertComposer; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; +import org.krews.apollyon.ftp.FTPUploadService; + import javax.imageio.ImageIO; import java.awt.image.BufferedImage; +import java.awt.image.DataBufferByte; import java.io.File; import java.io.IOException; @@ -53,7 +56,13 @@ public class CameraRoomThumbnailEvent extends MessageHandler } try { - ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.thumbnail") + room.getId() + ".png")); + if(Emulator.getConfig().getInt("ftp.enabled") == 1) { + byte[] imageBytes = ((DataBufferByte) theImage.getData().getDataBuffer()).getData(); + FTPUploadService.uploadImage(imageBytes, Emulator.getConfig().getValue("imager.location.output.thumbnail") + room.getId() + ".png"); + } + else { + ImageIO.write(theImage, "png", new File(Emulator.getConfig().getValue("imager.location.output.thumbnail") + room.getId() + ".png")); + } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) {