1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-28 08:10:11 +02:00

Merge pull request #998 from cyian-1756/redditShortNames

Handle File name too long file system errors
This commit is contained in:
cyian-1756
2018-10-12 00:36:54 -05:00
committed by GitHub

View File

@@ -1,11 +1,6 @@
package com.rarchives.ripme.ripper;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
@@ -210,7 +205,26 @@ class DownloadFileThread extends Thread {
if (statusCode == 206) {
fos = new FileOutputStream(saveAs, true);
} else {
fos = new FileOutputStream(saveAs);
try {
fos = new FileOutputStream(saveAs);
} catch (FileNotFoundException e) {
// We do this because some filesystems have a max name length
if (e.getMessage().contains("File name too long")) {
logger.error("The filename " + saveAs.getName() + " is to long to be saved on this file system.");
logger.info("Shortening filename");
String[] saveAsSplit = saveAs.getName().split("\\.");
// Get the file extension so when we shorten the file name we don't cut off the file extension
String fileExt = saveAsSplit[saveAsSplit.length - 1];
// The max limit for filenames on Linux with Ext3/4 is 255 bytes, on windows it's 256 chars so rather than
// bother with code with both platforms we just cut the file name down to 254 chars
logger.info(saveAs.getName().substring(0, 254 - fileExt.length()) + fileExt);
String filename = saveAs.getName().substring(0, 254 - fileExt.length()) + "." + fileExt;
// We can't just use the new file name as the saveAs because the file name doesn't include the
// users save path, so we get the user save path from the old saveAs
saveAs = new File(saveAs.getParentFile().getAbsolutePath() + "/" + filename);
fos = new FileOutputStream(saveAs);
}
}
}
byte[] data = new byte[1024 * 256];
int bytesRead;