diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index a738259..d68887d 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -1,5 +1,6 @@ package org.openslx.imagemaster.serverconnection; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -126,23 +127,21 @@ public class ImageProcessor } // insert new image - if ( DbImage.exists( imageData.uuid ) ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing." ); - } if ( !CrcFile.sumsAreValid( crcSums ) ) throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); - filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; // TODO: needs to be saved in another way... (Because of the versions.) + filepath = generateFilepathOfImage( imageData, !isUpdate ); token = RandomString.generate( 100, false ); nBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize ); allBlocks = new int[ nBlocks ]; // initialize array with all zeros which mean that this block is missing image = new UploadingImage( token, allBlocks, System.currentTimeMillis(), uuid, filepath ); + image.setDbImage( i ); // set the dbImage (it doesn't matter if the image is null because the uploadingImage is creating it then uploadingImages.put( uuid, image ); } CrcFile crcFile; try { // try to write crc file ... - crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); //TODO: same here ^ + crcFile = CrcFile.writeCrcFile( crcSums, generateFilepathOfCrcFile( imageData ) ); } catch ( IOException e ) { // ... and keep it in ram if it fails crcFile = new CrcFile( crcSums ); @@ -151,7 +150,7 @@ public class ImageProcessor ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image; if ( isUpdate ) { - // TODO: update db + i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); } else { DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath ); } @@ -298,6 +297,33 @@ public class ImageProcessor // for the downloader return null; } + + /** + * Generates the filePath of an image. + * And creates the folder if wanted. + * The crc file is found under filePath + ".crc" + * + * @param imageData The data of the image + * @param createFolder If you want the folder to be created + * @return The filePath of the given image + */ + public static String generateFilepathOfImage( ImageData imageData, boolean createFolder ) + { + String result = Globals.getImageDir() + "/" + imageData.uuid + "/"; + if ( createFolder ) { + File f = new File( result ); + if ( !f.exists() ) { + f.mkdirs(); + } + } + result += imageData.imageName + "-v" + String.valueOf( imageData.imageVersion ) + ".vmdk"; + return result; + } + + public static String generateFilepathOfCrcFile( ImageData imageData ) + { + return generateFilepathOfImage( imageData, false ) + ".crc"; + } /** * Checks pending uploads in database and adds them to process list again. @@ -312,7 +338,7 @@ public class ImageProcessor try { if ( !crcFile.isValid() ) { continue; - // UploadingImage object contains an CRCFile = null which invokes the ImageProcessor to retry to save it + // UploadingImage object will contain a CRCFile = null which invokes the ImageProcessor to retry to save it } } catch ( IOException e ) { continue; @@ -324,6 +350,8 @@ public class ImageProcessor } log.info( "Added " + list.size() + " pending upload(s) to process list again." ); } + + public static void init() { |