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 | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index d68887d..d5e253c 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -81,19 +81,20 @@ public class ImageProcessor } else if ( !DbUser.exists( imageData.imageOwner ) ) { throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." ); } - - DbImage i; - boolean isUpdate = false; - if ( ( i = DbImage.getImageByUuid( imageData.uuid ) ) != null ) { - // image is already available - // is the client updating?? - if ( imageData.imageVersion <= i.imageVersion ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "This image with the same or a newer version is already available." ); - } else { - // TODO: update db and prepare for new image file - isUpdate = true; - } - } + + //TODO: this is not working like this: + DbImage i = DbImage.getImageByUuid( imageData.uuid ); +// boolean isUpdate = false; +// if ( i != null ) { +// // image is already available +// // is the client updating?? +// if ( imageData.imageVersion <= i.imageVersion ) { +// throw new ImageDataException( ImageDataError.INVALID_DATA, "This image with the same or a newer version is already available." ); +// } else { +// // TODO: update db and prepare for new image file +// isUpdate = true; +// } +// } log.debug( serverSessionId + " is submitting " + imageData.uuid ); @@ -111,7 +112,7 @@ public class ImageProcessor CrcFile crcFile; try { // try to write crc file ... - crcFile = CrcFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); + crcFile = CrcFile.writeCrcFile( crcSums, generateFilepathOfCrcFile( imageData ) ); } catch ( IOException e ) { // ... and keep it in ram if it fails crcFile = new CrcFile( crcSums ); @@ -129,7 +130,7 @@ public class ImageProcessor // insert new image if ( !CrcFile.sumsAreValid( crcSums ) ) throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); - filepath = generateFilepathOfImage( imageData, !isUpdate ); + filepath = generateFilepathOfImage( imageData ); 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 @@ -149,11 +150,11 @@ public class ImageProcessor image.setCrcFile( crcFile ); ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image; - if ( isUpdate ) { - i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); - } else { +// if ( isUpdate ) { +// i.updateVersion( i.imageVersion, Util.getNumberOfBlocks( i.fileSize, Globals.blockSize ) ); +// } else { DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath ); - } +// } imagesToCheck.add( uuid ); log.debug( imagesToCheck ); @@ -237,7 +238,6 @@ public class ImageProcessor int got = 0; for ( int i = 0; i < image.getNumberOfBlocks(); i++ ) { - log.debug( i ); if ( image.needsRequest( i ) ) { int times = image.getTimesTransmitted( i ); if ( times > Globals.getSslTransmitTimes() ) { @@ -287,7 +287,6 @@ public class ImageProcessor log.debug( imagesToCheck ); while ( iter.hasNext() ) { result.add( uploadingImages.get( iter.next() ) ); - log.debug( "Added an image ... " ); } return result; } @@ -295,6 +294,7 @@ public class ImageProcessor public static List<Integer> getRequestedBlocks( String token ) { // for the downloader + // TODO return null; } @@ -307,22 +307,29 @@ public class ImageProcessor * @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 ) + public static String generateFilepathOfImage( ImageData imageData ) { - String result = Globals.getImageDir() + "/" + imageData.uuid + "/"; - if ( createFolder ) { - File f = new File( result ); - if ( !f.exists() ) { - f.mkdirs(); - } + return generateFilePathOfImage( imageData.uuid, imageData.imageName, imageData.imageVersion ); + } + + public static String generateFilePathOfImage( String uuid, String imageName, int imageVersion) { + String result = Globals.getImageDir() + "/" + uuid + "/"; + File dir = new File(result); + if ( !dir.exists() ) { + dir.mkdirs(); } - result += imageData.imageName + "-v" + String.valueOf( imageData.imageVersion ) + ".vmdk"; + result += imageName + "-v" + String.valueOf( imageVersion ) + ".vmdk"; return result; } public static String generateFilepathOfCrcFile( ImageData imageData ) { - return generateFilepathOfImage( imageData, false ) + ".crc"; + return generateFilepathOfImage( imageData ) + ".crc"; + } + + public static String generateFilepathOfCrcFile( String uuid, String imageName, int imageVersion ) + { + return generateFilePathOfImage( uuid, imageName, imageVersion ) + ".crc"; } /** @@ -334,7 +341,7 @@ public class ImageProcessor for ( DbImage image : list ) { UploadingImage infos = new UploadingImage( image.token, image.blockStatus, image.timestamp.getTime(), image.uuid, image.imagePath ); ConnectionHandler.addConnection( image.token, image.imagePath, Connection.UPLOADING ).image = infos; - CrcFile crcFile = new CrcFile( Globals.getImageDir() + "/" + image.uuid + ".crc" ); // TODO: has to be adjusted with the corresponding value above + CrcFile crcFile = new CrcFile( generateFilepathOfCrcFile( image.uuid, image.imageName, image.imageVersion ) ); // TODO: has to be adjusted with the corresponding value above try { if ( !crcFile.isValid() ) { continue; |