From d684cd4dbdadb11a0017556e802bdf3141336f2b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 10 Jun 2015 20:22:04 +0200 Subject: [server] db stuff, new interface, ... --- .../src/main/java/fileserv/ActiveUpload.java | 60 ++++++++++++++++++++-- .../src/main/java/fileserv/FileServer.java | 11 ++-- 2 files changed, 63 insertions(+), 8 deletions(-) (limited to 'dozentenmodulserver/src/main/java/fileserv') diff --git a/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java b/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java index 256f8b8d..334345f3 100644 --- a/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java +++ b/dozentenmodulserver/src/main/java/fileserv/ActiveUpload.java @@ -6,14 +6,21 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.util.List; +import java.util.UUID; import java.util.concurrent.ThreadPoolExecutor; +import models.Configuration; + import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.ImageDetailsRead; +import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.filetransfer.DataReceivedCallback; import org.openslx.filetransfer.Downloader; import org.openslx.filetransfer.FileRange; import org.openslx.filetransfer.WantRangeCallback; -import org.openslx.imagemaster.thrift.iface.UserInfo; + +import util.FileSystem; +import util.Formatter; public class ActiveUpload { private static final Logger LOGGER = Logger.getLogger(ActiveUpload.class); @@ -36,14 +43,20 @@ public class ActiveUpload { */ private final UserInfo owner; + /** + * Base image this upload is a new version for. + */ + private final ImageDetailsRead image; + // TODO: Use HashList for verification - public ActiveUpload(UserInfo owner, File destinationFile, long fileSize, List sha1Sums) - throws FileNotFoundException { + public ActiveUpload(UserInfo owner, ImageDetailsRead image, File destinationFile, long fileSize, + List sha1Sums) throws FileNotFoundException { this.destinationFile = destinationFile; this.outFile = new RandomAccessFile(destinationFile, "rw"); this.chunks = new ChunkList(fileSize, sha1Sums); this.owner = owner; + this.image = image; this.fileSize = fileSize; } @@ -96,6 +109,47 @@ public class ActiveUpload { return true; } + private void finishUpload() { + File file = destinationFile; + // Ready to go. First step: Rename temp file to something usable + File destination = new File(file.getParent(), Formatter.vmName(owner, image.imageName)); + // Sanity check: destination should be a sub directory of the vmStorePath + String relPath = FileSystem.getRelativePath(destination, Configuration.getVmStoreBasePath()); + if (relPath == null) { + LOGGER.warn(destination.getAbsolutePath() + " is not a subdir of " + + Configuration.getVmStoreBasePath().getAbsolutePath()); + // TODO: Update state to failed... + } + + // Execute rename + boolean ret = false; + Exception renameException = null; + try { + ret = file.renameTo(destination); + } catch (Exception e) { + ret = false; + renameException = e; + } + if (!ret) { + // Rename failed :-( + LOGGER.warn( + "Could not rename '" + file.getAbsolutePath() + "' to '" + destination.getAbsolutePath() + + "'", renameException); + // TODO: Update state.... + } + + // Now insert meta data into DB + + final String imageVersionId = UUID.randomUUID().toString(); + + // TODO: SQL magic, update state + } + + public void cancel() { + // TODO Auto-generated method stub + + } + /** * Get user owning this upload. Can be null in special cases. * diff --git a/dozentenmodulserver/src/main/java/fileserv/FileServer.java b/dozentenmodulserver/src/main/java/fileserv/FileServer.java index 8322e2e9..e82fa39c 100644 --- a/dozentenmodulserver/src/main/java/fileserv/FileServer.java +++ b/dozentenmodulserver/src/main/java/fileserv/FileServer.java @@ -10,12 +10,12 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import org.openslx.bwlp.thrift.iface.TTransferRejectedException; +import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.filetransfer.Downloader; import org.openslx.filetransfer.IncomingEvent; import org.openslx.filetransfer.Listener; import org.openslx.filetransfer.Uploader; -import org.openslx.imagemaster.thrift.iface.UserInfo; -import org.openslx.sat.thrift.iface.TUploadRejectedException; import util.Constants; import util.Formatter; @@ -70,7 +70,7 @@ public class FileServer implements IncomingEvent { } public String createNewUserUpload(UserInfo owner, long fileSize, List sha1Sums) - throws TUploadRejectedException, FileNotFoundException { + throws TTransferRejectedException, FileNotFoundException { Iterator it = uploads.values().iterator(); int activeUploads = 0; while (it.hasNext()) { @@ -84,12 +84,13 @@ public class FileServer implements IncomingEvent { activeUploads++; } if (activeUploads > Constants.MAX_UPLOADS) - throw new TUploadRejectedException("Server busy. Too many running uploads."); + throw new TTransferRejectedException("Server busy. Too many running uploads."); File destinationFile = null; do { destinationFile = Formatter.getTempImageName(); } while (destinationFile.exists()); - ActiveUpload upload = new ActiveUpload(owner, destinationFile, fileSize, sha1Sums); + // TODO: Pass image + ActiveUpload upload = new ActiveUpload(owner, null, destinationFile, fileSize, sha1Sums); String key = UUID.randomUUID().toString(); uploads.put(key, upload); return key; -- cgit v1.2.3-55-g7522