diff options
author | Nils Schwabe | 2014-07-14 14:55:09 +0200 |
---|---|---|
committer | Nils Schwabe | 2014-07-14 14:55:09 +0200 |
commit | 6a21ad5c5674f64c646860648f2a3343458b7006 (patch) | |
tree | a91172440aa69deb2060b1c7bc50302f8162c162 /src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | |
parent | Add security checks for image upload (diff) | |
download | masterserver-6a21ad5c5674f64c646860648f2a3343458b7006.tar.gz masterserver-6a21ad5c5674f64c646860648f2a3343458b7006.tar.xz masterserver-6a21ad5c5674f64c646860648f2a3343458b7006.zip |
Add feature to hold crc sums in RAM when writing to disk fails
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java index f40aece..9758a2c 100644 --- a/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/serverconnection/ImageProcessor.java @@ -70,23 +70,22 @@ public class ImageProcessor { // check image data if ( DbImage.exists( imageData.uuid ) ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing."); + throw new ImageDataException( ImageDataError.INVALID_DATA, "UUID already existing." ); } else if ( imageData.imageName == null || imageData.imageName.isEmpty() ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "Image name not set."); + throw new ImageDataException( ImageDataError.INVALID_DATA, "Image name not set." ); } else if ( imageData.imageName == null || imageData.imageOwner.isEmpty() ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "Image owner not set."); + throw new ImageDataException( ImageDataError.INVALID_DATA, "Image owner not set." ); } else if ( imageData.contentOperatingSystem == null || imageData.contentOperatingSystem.isEmpty() ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "Content operating system not set."); + throw new ImageDataException( ImageDataError.INVALID_DATA, "Content operating system not set." ); } else if ( imageData.fileSize <= 0 ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "File size is too small."); + throw new ImageDataException( ImageDataError.INVALID_DATA, "File size is too small." ); } else if ( !DbUser.exists( imageData.imageOwner ) ) { - throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." ); + throw new ImageDataException( ImageDataError.INVALID_DATA, "User is not known." ); } String uuid = imageData.uuid; String token; String filepath; - String crcPath; int nBlocks; int[] allBlocks; UploadingImage image; @@ -104,22 +103,25 @@ public class ImageProcessor } // insert new image - if ( !CRCFile.sumsAreValid( crcSums ) ) throw new UploadException(UploadError.INVALID_CRC, "CRC sums were invalid."); + if ( !CRCFile.sumsAreValid( crcSums ) ) + throw new UploadException( UploadError.INVALID_CRC, "CRC sums were invalid." ); filepath = Globals.getImageDir() + "/" + uuid + ".vmdk"; token = RandomString.generate( 100, false ); - crcPath = Globals.getImageDir() + "/" + uuid + ".crc"; nBlocks = (int)Math.ceil( imageData.fileSize / Globals.blockSize ); - allBlocks = new int[nBlocks]; // initalize array with all zeros which mean that this block is missing - image = new UploadingImage( token, allBlocks, new Timestamp( System.currentTimeMillis() ), uuid, filepath, crcPath ); + allBlocks = new int[ nBlocks ]; // initalize array with all zeros which mean that this block is missing + image = new UploadingImage( token, allBlocks, new Timestamp( System.currentTimeMillis() ), uuid, filepath ); uploadingImages.put( uuid, image ); } - - try { - CRCFile.writeCrcFile( crcSums, crcPath ); - } catch (IOException e) { - log.error( "Could not create crc file", e ); - return null; // TODO: what to do if we can not write the crc file to disk? Give object to crcscheduler? - } + + CRCFile crcFile; + try { + // try to write crc file ... + crcFile = CRCFile.writeCrcFile( crcSums, Globals.getImageDir() + "/" + uuid + ".crc" ); + } catch ( IOException e ) { + // ... and keep it in ram if it fails + crcFile = new CRCFile( crcSums ); + } + image.setCrcFile( crcFile ); ConnectionHandler.addConnection( token, filepath, Connection.UPLOADING ).image = image; DbImage.insert( imageData, System.currentTimeMillis(), token, nBlocks, serverSessionId, filepath ); @@ -168,7 +170,7 @@ public class ImageProcessor DownloadingClient client = new DownloadingClient(); client.addDownload( uuid, requestedBlocks, token ); downloadingClients.put( serverSessionId, client ); - + ConnectionHandler.addConnection( token, filepath, Connection.DOWNLOADING ).client = client; return new DownloadInfos( token, Globals.getSslSocketPort() ); } @@ -203,13 +205,14 @@ public class ImageProcessor int got = 0; for ( int i = 0; i < image.getNumberOfBlocks(); i++ ) { - if (image.needsRequest( i )) { - result.add( i ) ; + if ( image.needsRequest( i ) ) { + result.add( i ); got++; } - if (got == amount) break; + if ( got == amount ) + break; } - + return result; } @@ -224,7 +227,7 @@ public class ImageProcessor synchronized ( imagesToCheck ) { imagesToCheck.remove( uuid ); } - + UploadingImage image; synchronized ( uploadingImages ) { image = uploadingImages.remove( uuid ); @@ -244,12 +247,13 @@ public class ImageProcessor } return result; } - - public static List<Integer> getRequestedBlocks( String token) { + + public static List<Integer> getRequestedBlocks( String token ) + { // for the downloader return null; } - + /** * Checks pending uploads in database and adds them to process list again. */ @@ -260,7 +264,15 @@ public class ImageProcessor String token = image.token; ConnectionHandler.addConnection( token, image.imagePath, Connection.UPLOADING ); UploadingImage infos = new UploadingImage( token, image.blockStatus, image.timestamp, - image.uuid, image.imagePath, Globals.getImageDir() + "/" + image.uuid + ".crc" ); + image.uuid, image.imagePath ); + CRCFile crcFile = new CRCFile( Globals.getImageDir() + "/" + image.uuid + ".crc" ); + try { + if ( !crcFile.isValid() ) + continue; + } catch ( IOException e ) { + continue; + } + infos.setCrcFile( crcFile ); uploadingImages.put( image.uuid, infos ); } log.info( "Added " + list.size() + " pending upload(s) to process list again." ); |