2022-04-18 03:05:20 +00:00
package dev.plex.request ;
2024-01-25 21:59:27 +00:00
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat ;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats ;
2022-04-18 03:05:20 +00:00
import dev.plex.HTTPDModule ;
2022-04-18 06:36:45 +00:00
import dev.plex.cache.DataUtils ;
import dev.plex.player.PlexPlayer ;
import dev.plex.util.PlexLog ;
2022-04-18 03:05:20 +00:00
import jakarta.servlet.ServletException ;
import jakarta.servlet.http.HttpServlet ;
import jakarta.servlet.http.HttpServletRequest ;
import jakarta.servlet.http.HttpServletResponse ;
import jakarta.servlet.http.Part ;
2024-01-25 21:59:27 +00:00
import org.apache.commons.io.FileUtils ;
import org.bukkit.Bukkit ;
import org.bukkit.OfflinePlayer ;
2022-04-18 03:05:20 +00:00
import java.io.File ;
2024-01-25 21:59:27 +00:00
import java.io.FileInputStream ;
2022-04-18 03:05:20 +00:00
import java.io.IOException ;
import java.io.InputStream ;
import java.util.Arrays ;
import java.util.regex.Pattern ;
public class SchematicUploadServlet extends HttpServlet
{
private static final Pattern schemNameMatcher = Pattern . compile ( " ^[a-z0-9'!,_ -]{1,30} \\ .schem(atic)?$ " , Pattern . CASE_INSENSITIVE ) ;
@Override
2022-04-18 06:36:45 +00:00
protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException
{
if ( request . getRemoteAddr ( ) = = null )
{
response . getWriter ( ) . println ( schematicUploadBadHTML ( " Your IP address could not be detected. Please ensure you are using IPv4. " ) ) ;
return ;
}
PlexPlayer plexPlayer = DataUtils . getPlayerByIP ( request . getRemoteAddr ( ) ) ;
if ( plexPlayer = = null )
{
response . getWriter ( ) . println ( schematicUploadBadHTML ( " Couldn't load your IP Address: " + request . getRemoteAddr ( ) + " . Have you joined the server before? " ) ) ;
return ;
}
2023-08-29 00:02:46 +00:00
PlexLog . debug ( " Plex-HTTPD using permissions check " ) ;
final OfflinePlayer offlinePlayer = Bukkit . getOfflinePlayer ( plexPlayer . getUuid ( ) ) ;
if ( ! HTTPDModule . getPermissions ( ) . playerHas ( null , offlinePlayer , " plex.httpd.schematics.upload " ) )
2022-04-18 06:36:45 +00:00
{
2023-08-29 00:02:46 +00:00
response . getWriter ( ) . println ( schematicUploadBadHTML ( " You do not have permission to upload schematics. " ) ) ;
return ;
2022-04-18 06:36:45 +00:00
}
2022-04-18 03:05:20 +00:00
File worldeditFolder = HTTPDModule . getWorldeditFolder ( ) ;
if ( worldeditFolder = = null )
{
2022-04-18 06:36:45 +00:00
response . getWriter ( ) . println ( schematicUploadBadHTML ( " Worldedit is not installed! " ) ) ;
2022-04-18 03:05:20 +00:00
return ;
}
File [ ] schematics = worldeditFolder . listFiles ( ) ;
2022-04-18 03:09:53 +00:00
Part uploadPart ;
2022-04-18 03:05:20 +00:00
try
{
2022-04-18 06:36:45 +00:00
uploadPart = request . getPart ( " file " ) ;
2022-04-18 03:05:20 +00:00
}
catch ( IllegalStateException e )
{
2022-04-18 06:36:45 +00:00
response . getWriter ( ) . println ( schematicUploadBadHTML ( " That schematic is too large! " ) ) ;
2022-04-18 03:05:20 +00:00
return ;
}
String filename = uploadPart . getSubmittedFileName ( ) . replaceAll ( " [^a-zA-Z0-9'!,_ .-] " , " _ " ) ;
2022-04-18 03:09:53 +00:00
if ( ! schemNameMatcher . matcher ( filename ) . matches ( ) )
2022-04-18 03:05:20 +00:00
{
2022-04-18 06:36:45 +00:00
response . getWriter ( ) . println ( schematicUploadBadHTML ( " That is not a valid schematic filename! " ) ) ;
2022-04-18 03:09:53 +00:00
return ;
}
boolean alreadyExists = schematics ! = null & & Arrays . stream ( schematics ) . anyMatch ( file - > HTTPDModule . fileNameEquals ( file . getName ( ) , filename ) ) ;
if ( alreadyExists )
{
2022-04-18 06:36:45 +00:00
response . getWriter ( ) . println ( schematicUploadBadHTML ( " A schematic with the name <b> " + filename + " </b> already exists! " ) ) ;
2022-04-18 03:09:53 +00:00
return ;
2022-04-18 03:05:20 +00:00
}
2022-04-18 03:09:53 +00:00
InputStream inputStream = uploadPart . getInputStream ( ) ;
2024-01-25 21:59:27 +00:00
File schematicFile = new File ( worldeditFolder , filename ) ;
FileUtils . copyInputStreamToFile ( inputStream , schematicFile ) ;
ClipboardFormat schematicFormat = ClipboardFormats . findByFile ( schematicFile ) ;
if ( schematicFormat = = null )
{
PlexLog . log ( " IP Address: " + request . getRemoteAddr ( ) + " FAILED to upload schematic with filename: " + filename ) ;
response . getWriter ( ) . println ( schematicUploadBadHTML ( " Schematic is not a valid format. " ) ) ;
FileUtils . deleteQuietly ( schematicFile ) ;
return ;
}
try
{
schematicFormat . getReader ( new FileInputStream ( schematicFile ) ) ;
}
catch ( IOException e )
{
PlexLog . log ( " IP Address: " + request . getRemoteAddr ( ) + " FAILED to upload schematic with filename: " + filename ) ;
response . getWriter ( ) . println ( schematicUploadBadHTML ( " Schematic is not a valid format. " ) ) ;
FileUtils . deleteQuietly ( schematicFile ) ;
return ;
}
// Files.copy(inputStream, schematic.toPath(), StandardCopyOption.REPLACE_EXISTING);
2022-04-18 03:09:53 +00:00
inputStream . close ( ) ;
2024-01-25 21:59:27 +00:00
response . getWriter ( ) . println ( schematicUploadGoodHTML ( " Successfully uploaded <b> " + filename + " </b>. " ) ) ;
PlexLog . log ( " IP Address: " + request . getRemoteAddr ( ) + " uploaded schematic with filename: " + filename ) ;
2022-04-18 03:05:20 +00:00
}
private String schematicUploadBadHTML ( String message )
{
String file = AbstractServlet . readFile ( this . getClass ( ) . getResourceAsStream ( " /httpd/schematic_upload_bad.html " ) ) ;
file = file . replace ( " ${MESSAGE} " , message ) ;
return file ;
}
private String schematicUploadGoodHTML ( String message )
{
String file = AbstractServlet . readFile ( this . getClass ( ) . getResourceAsStream ( " /httpd/schematic_upload_good.html " ) ) ;
file = file . replace ( " ${MESSAGE} " , message ) ;
return file ;
}
}