From 1bc83891c68ee269727e81a13cc70da698bcc7a7 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 2 Jun 2015 19:53:31 +0200 Subject: [server] Compiling again, still lots of stubs --- .../src/main/java/util/Formatter.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 dozentenmodulserver/src/main/java/util/Formatter.java (limited to 'dozentenmodulserver/src/main/java/util/Formatter.java') diff --git a/dozentenmodulserver/src/main/java/util/Formatter.java b/dozentenmodulserver/src/main/java/util/Formatter.java new file mode 100644 index 00000000..5c5982fb --- /dev/null +++ b/dozentenmodulserver/src/main/java/util/Formatter.java @@ -0,0 +1,59 @@ +package util; + +import java.io.File; +import java.util.UUID; + +import models.Configuration; + +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.openslx.imagemaster.thrift.iface.UserInfo; + +public class Formatter { + + private static final DateTimeFormatter vmNameDateFormat = DateTimeFormat.forPattern("dd_HH-mm-ss"); + + /** + * Generate a unique file name used for a virtual machine + * image that is currently uploading. + * + * @return Absolute path name of file + */ + public static File getTempImageName() { + return new File(Configuration.getCurrentVmStorePath(), UUID.randomUUID().toString() + + Constants.INCOMPLETE_UPLOAD_SUFFIX); + } + + /** + * Generate a file name for the given VM based on owner and display name. + * + * @param user The user associated with the VM, e.g. the owner + * @param imageName Name of the VM + * @return File name for the VM derived from the function's input + */ + public static String vmName(UserInfo user, String imageName) { + return cleanFileName(vmNameDateFormat.print(System.currentTimeMillis()) + "_" + user.lastName + "_" + + imageName); + } + + /** + * Make sure file name contains only a subset of ascii characters and is not + * too long. + * + * @param name What we want to turn into a file name + * @return A sanitized form of name that should be safe to use as a file + * name + */ + public static String cleanFileName(String name) { + if (name == null) + return "null"; + name = name.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_"); + if (name.length() > 120) + name = name.substring(0, 120); + return name; + } + + public static String userFullName(UserInfo ui) { + return ui.firstName + " " + ui.lastName; + } +} -- cgit v1.2.3-55-g7522