Media mediaRow;
try {
mediaRow = db.getMedia(h);
} catch(DBConnectionException e) {
throw new ContentStoreException("Lost connection to database, can't reconnect", e);
}
// Media is banned from archiving
if(mediaRow.getBanned() == 1) return;
// Get the proper filename for the file type we're outputting
String filename;
if(this.useOldDirectoryStructure)
filename = isPreview ? h.getPreview() : h.getMedia();
else
filename = isPreview ? (h.isOp() ? mediaRow.getPreviewOp() : mediaRow.getPreviewReply()) :
mediaRow.getMedia();
if(filename == null) return;
// Create the dir structure (if necessary) and return the path to where we're outputting our file
// Filename is enough for us here, we just need the first part of the string
String outputDir;
if(this.useOldDirectoryStructure)
outputDir = makeDir(h, isPreview ? DIR_THUMB : DIR_MEDIA, FULL_FILE);
else
outputDir = makeDir(filename, isPreview ? DIR_THUMB : DIR_MEDIA, FULL_FILE);
// Construct the path and back down if the file already exists
File outputFile = new File(outputDir + "/" + filename);
if(outputFile.exists()) {
if (!isPreview) outputFile.setLastModified(System.currentTimeMillis());
return;
}
// Open a temp file for writing
String tempFilePath;
if(this.useOldDirectoryStructure)
tempFilePath = makeDir(h, isPreview ? DIR_THUMB : DIR_MEDIA, TEMP_FILE);
else
tempFilePath = makeDir(filename, isPreview ? DIR_THUMB : DIR_MEDIA, TEMP_FILE);
// Throws ContentGetException on failure
InputStream inStream = isPreview ? source.getMediaPreview(h) : source.getMedia(h);
OutputStream outFile = null;
File tempFile = null;
try {
tempFile = File.createTempFile(filename + "_", null, new File(tempFilePath + "/"));
outFile = new BufferedOutputStream(new FileOutputStream(tempFile));
// Copy the network input stream to our local file
// In case the connection is cut off or something similar happens, an IOException
// will be thrown.
ByteStreams.copy(inStream, outFile);
} catch(FileNotFoundException e) {
throw new ContentStoreException("The temp file we just created wasn't there!", e);
} catch(IOException e) {
if(tempFile != null && !tempFile.delete())
System.err.println("Additionally, temporary file " + tempFilePath + "/" + filename + " could not be deleted.");
e.printStackTrace();
throw new ContentStoreException("IOException in file download", e);
} finally {
try {
if(outFile != null) outFile.close();
inStream.close();
} catch(IOException e) {
System.err.println("IOException trying to close streams after file download");
e.printStackTrace();
}
}
// Move the temporary file into place
if(!tempFile.renameTo(outputFile))
throw new ContentStoreException("Unable to move temporary file " + tempFilePath + "/" + filename + " into place");
try {
if(this.webGroupId != 0) {
posix.chmod(outputFile.getCanonicalPath(), 0664);
posix.chown(outputFile.getCanonicalPath(), -1, this.webGroupId);
}
} catch(IOException e) {
throw new ContentStoreException("IOException trying to get filename for output file (nice broken filesystem you have there)", e);
}
}