diff options
author | Simon Rettberg | 2014-09-25 14:50:56 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-09-25 14:50:56 +0200 |
commit | 8b87677709624a56a7557104dc31ee8cc2ece748 (patch) | |
tree | 570d9bf3216ea9aa2c5c4158409ec3475e246963 /src/main/java/org/openslx/imagemaster/db | |
parent | Adapted classes to new filetransfer. (diff) | |
download | masterserver-8b87677709624a56a7557104dc31ee8cc2ece748.tar.gz masterserver-8b87677709624a56a7557104dc31ee8cc2ece748.tar.xz masterserver-8b87677709624a56a7557104dc31ee8cc2ece748.zip |
Adapt to changed Thrift RPC
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/DbImage.java | 88 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/db/DbUser.java | 63 |
2 files changed, 82 insertions, 69 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java index dc2ab34..a7a2adb 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbImage.java +++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java @@ -1,8 +1,6 @@ package org.openslx.imagemaster.db; import java.sql.Timestamp; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import org.openslx.imagemaster.Globals; @@ -20,22 +18,22 @@ public class DbImage public final String uuid; public final int imageVersion; public final String imageName; + /** + * Relative path of image file (relative to Globals.getImageDir()) + */ public final String imagePath; public final Timestamp imageCreateTime; public final Timestamp imageUpdateTime; - public final String imageOwner; + public final int imageOwnerId; public final String contentOperatingSystem; public final boolean isValid; public final boolean isDeleted; public final String shortDescription; public final String longDescription; - public final Timestamp timestamp; public final long fileSize; - public final String token; public final int[] blockStatus; - public final String serverSessionId; - public DbImage(String uuid) + public DbImage( String uuid ) { this.uuid = uuid; this.imageVersion = 0; @@ -43,23 +41,20 @@ public class DbImage this.imagePath = null; this.imageCreateTime = null; this.imageUpdateTime = null; - this.imageOwner = null; + this.imageOwnerId = 0; this.contentOperatingSystem = null; this.isValid = false; this.isDeleted = false; this.shortDescription = null; this.longDescription = null; - this.timestamp = new Timestamp( 0 ); this.fileSize = 0; - this.token = null; this.blockStatus = null; - this.serverSessionId = null; } - public DbImage(String uuid, int imageVersion, String imageName, String imagePath, - Timestamp imageCreateTime, Timestamp imageUpdateTime, String imageOwner, String contentOperatingSystem, + public DbImage( String uuid, int imageVersion, String imageName, String imagePath, + Timestamp imageCreateTime, Timestamp imageUpdateTime, int imageOwnerId, String contentOperatingSystem, boolean isValid, boolean isDeleted, String shortDescription, String longDescription, - Timestamp timestamp, long fileSize, String token, String missingBlocksList, String serverSessionId) + long fileSize, String missingBlocksList ) { this.uuid = uuid; this.imageVersion = imageVersion; @@ -67,26 +62,24 @@ public class DbImage this.imagePath = imagePath; this.imageCreateTime = imageCreateTime; this.imageUpdateTime = imageUpdateTime; - this.imageOwner = imageOwner; + this.imageOwnerId = imageOwnerId; this.contentOperatingSystem = contentOperatingSystem; this.isValid = isValid; this.isDeleted = isDeleted; this.shortDescription = shortDescription; this.longDescription = longDescription; - this.timestamp = timestamp; this.fileSize = fileSize; - this.token = token; String[] parts = missingBlocksList.split( ";" ); blockStatus = new int[ Util.getNumberOfBlocks( fileSize, Globals.blockSize ) ]; // initialize array to ones - for ( int i : blockStatus ) { - blockStatus[i] = UploadingImage.valid; + for ( int i = 0; i < blockStatus.length; ++i ) { + blockStatus[i] = UploadingImage.VALID; } - for ( int i = 0; i < parts.length; i++ ) { - blockStatus[i] = UploadingImage.missing; + for ( String block : parts ) { // Now mark missing blocks (if any) + int i = Util.tryToParseInt( block, -1 ); + if ( i >= 0 && i < blockStatus.length ) + blockStatus[i] = UploadingImage.MISSING; } - - this.serverSessionId = serverSessionId; } /** @@ -104,38 +97,30 @@ public class DbImage * Insert a new image into database * * @param imageData The metadata of the image - * @param ts The timestamp of inserting - * @param token The token that is able to upload this image - * @param amountBlocks The blocks that are missing for this image - * @param serverSessionId The server that is uploading this image + * @param filepath Local storage path of image * @return Affected rows */ - public static int insert( ImageData imageData, long ts, String token, int amountBlocks, String serverSessionId, String filepath ) + public static int insert( ImageData imageData, String filepath ) { - Date createTime = new Date( imageData.imageCreateTime ); - Date updateTime = new Date( imageData.imageUpdateTime ); - Date timestamp = new Date( ts ); - SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); - + int numBlocks = Util.getNumberOfBlocks( imageData.fileSize, Globals.blockSize ); String missingBlocksList = ""; - for ( int i = 0; i < amountBlocks; i++ ) { + for ( int i = 0; i < numBlocks; i++ ) { missingBlocksList = missingBlocksList + String.valueOf( i ) + ";"; } + DbUser user = DbUser.forLogin( imageData.imageOwner ); + int owner = 0; + if (user != null) + owner = user.userId; return MySQL .update( - "INSERT INTO images (UUID, image_version, image_name, image_path, image_createTime, image_updateTime, image_owner, content_operatingSystem, status_isValid, status_isDeleted, image_shortDescription, image_longDescription, timestamp, fileSize, token, missingBlocks, serverSessionId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO images (UUID, image_version, image_name, image_path, image_createTime, image_updateTime, image_owner, content_operatingSystem, status_isValid, status_isDeleted, image_shortDescription, image_longDescription, fileSize, missingBlocks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", imageData.uuid, imageData.imageVersion, imageData.imageName, filepath, - sdf.format( createTime ), sdf.format( updateTime ), imageData.imageOwner, + imageData.imageCreateTime, imageData.imageUpdateTime, owner, imageData.contentOperatingSystem, imageData.statusIsValid, imageData.statusIsDeleted, imageData.imageShortDescription, - imageData.imageLongDescription, sdf.format( timestamp ), imageData.fileSize, - token, missingBlocksList, serverSessionId ); - } - - public String getUuid() - { - return this.uuid; + imageData.imageLongDescription, imageData.fileSize, + missingBlocksList ); } /** @@ -218,8 +203,23 @@ public class DbImage */ public ImageData getImageData() { + String owner = "unknown"; + DbUser user = DbUser.forLogin( this.imageOwnerId ); + if (user != null) + owner = user.getLogin(); return new ImageData( this.uuid, this.imageVersion, this.imageName, this.imageCreateTime.getTime(), - this.imageUpdateTime.getTime(), this.imageOwner, this.contentOperatingSystem, this.isValid, + this.imageUpdateTime.getTime(), owner, this.contentOperatingSystem, this.isValid, this.isDeleted, this.shortDescription, this.longDescription, this.fileSize ); } + + /** + * Get absolute path of this image + * + * @return absolute path + */ + public String getAbsolutePath() + { + return Globals.getImageDir() + "/" + this.imagePath; + } + } diff --git a/src/main/java/org/openslx/imagemaster/db/DbUser.java b/src/main/java/org/openslx/imagemaster/db/DbUser.java index 91e0615..c486da3 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbUser.java +++ b/src/main/java/org/openslx/imagemaster/db/DbUser.java @@ -2,6 +2,7 @@ package org.openslx.imagemaster.db; import org.apache.log4j.Logger; import org.openslx.imagemaster.session.User; +import org.openslx.imagemaster.util.Sha512Crypt; /** * Represents a user that can login against the masterserver. @@ -11,9 +12,9 @@ public class DbUser extends User private static Logger log = Logger.getLogger( DbUser.class ); - public DbUser(int userId, String username, String password, String organization, + public DbUser( int userId, String username, String password, String organization, String firstName, String lastName, String eMail, - String satelliteAddress) + String satelliteAddress ) { super( userId, username, password, organization, firstName, lastName, eMail, satelliteAddress ); @@ -41,6 +42,37 @@ public class DbUser extends User parts[0], parts[1] ); } + /** + * Query database for user with given userId + * + * @param userid + * @return instance of DbUser for matching entry from DB, or null if not + * found + */ + public static DbUser forLogin( final int userid ) + { + return MySQL + .findUniqueOrNull( + DbUser.class, + "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" + + " LEFT JOIN satellite USING (organization)" + + " WHERE user.userid = ? LIMIT 1", + userid ); + } + + public static boolean exists( final String login ) + { + return forLogin( login ) != null; + } + + public static DbUser forLogin( String login, String password ) + { + DbUser user = forLogin( login ); + if ( user == null || !Sha512Crypt.verifyPassword( password, user.password ) ) + return null; + return user; + } + public static boolean insertOrUpdate( User user ) { log.debug( "Inserted user '" + user.username + "' into db." ); @@ -58,7 +90,8 @@ public class DbUser extends User "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" + " LEFT JOIN satellite USING (organization)" + " WHERE user.username = ? LIMIT 1", username ); - if ( user == null ) return 0; + if ( user == null ) + return 0; return user.userId; } @@ -70,29 +103,9 @@ public class DbUser extends User "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" + " LEFT JOIN satellite USING (organization)" + " WHERE user.userid = ? LIMIT 1", id ); - if (user == null) return ""; + if ( user == null ) + return ""; return user.username; } - /** - * Checks if a user with id (userid@organization) exists - * @param id - * @return Whether the user exists - */ - public static boolean exists( String id ) - { - String[] parts = id.split( "@" ); - String user = parts[0]; - String organization = parts[1]; - - DbUser dbUser = MySQL.findUniqueOrNull( DbUser.class, - "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" - + " LEFT JOIN satellite USING (organization)" - + " WHERE user.username = ? AND user.organization = ? LIMIT 1", user, organization); - - if ( dbUser == null ) return false; - - return true; - } - } |