2014-11-20 13:21:19 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010-2014 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
2020-05-28 13:28:42 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
2014-11-20 13:21:19 +00:00
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; version 2
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.sheepit.client;
|
|
|
|
|
|
2021-05-11 15:36:59 +00:00
|
|
|
import com.sheepit.client.Error.ServerCode;
|
|
|
|
|
import net.lingala.zip4j.ZipFile;
|
|
|
|
|
import net.lingala.zip4j.exception.ZipException;
|
2023-09-19 17:14:49 +00:00
|
|
|
import net.lingala.zip4j.io.inputstream.ZipInputStream;
|
|
|
|
|
import net.lingala.zip4j.model.LocalFileHeader;
|
2021-05-11 15:36:59 +00:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
import org.w3c.dom.Element;
|
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
2020-07-07 15:37:15 +02:00
|
|
|
import java.io.BufferedInputStream;
|
2023-09-19 17:14:49 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.io.File;
|
2020-07-07 15:37:15 +02:00
|
|
|
import java.io.FileInputStream;
|
2023-09-19 17:14:49 +00:00
|
|
|
import java.io.FileOutputStream;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2018-06-30 01:41:54 +02:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
2020-07-07 15:37:15 +02:00
|
|
|
import java.net.URLConnection;
|
2015-03-05 20:27:13 +00:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
2015-01-28 00:46:08 +00:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
2023-01-06 14:53:06 +00:00
|
|
|
import java.util.HashMap;
|
2023-09-19 17:14:49 +00:00
|
|
|
import java.util.List;
|
2023-01-06 14:53:06 +00:00
|
|
|
import java.util.Map;
|
2015-01-28 00:46:08 +00:00
|
|
|
import java.util.TimeZone;
|
2014-11-20 13:21:19 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
public class Utils {
|
2023-01-06 14:53:06 +00:00
|
|
|
|
|
|
|
|
private static Map<String, String> mimeTypes = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
mimeTypes.put(".tga", "image/tga");
|
|
|
|
|
mimeTypes.put(".exr", "image/x-exr");
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 14:02:22 +00:00
|
|
|
public static int unzipFileIntoDirectory(String zipFileName_, String destinationDirectory, char[] password, Log log) {
|
2014-11-20 13:21:19 +00:00
|
|
|
try {
|
2016-10-05 23:01:03 +02:00
|
|
|
ZipFile zipFile = new ZipFile(zipFileName_);
|
2021-05-11 15:36:59 +00:00
|
|
|
// unzipParameters.setIgnoreDateTimeAttributes(true);
|
2016-10-05 23:01:03 +02:00
|
|
|
|
|
|
|
|
if (password != null && zipFile.isEncrypted()) {
|
|
|
|
|
zipFile.setPassword(password);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2021-05-11 15:36:59 +00:00
|
|
|
// zipFile.extractAll(destinationDirectory, unzipParameters);
|
|
|
|
|
zipFile.extractAll(destinationDirectory);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
2016-10-05 23:01:03 +02:00
|
|
|
catch (ZipException e) {
|
2018-06-30 01:41:54 +02:00
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
log.debug("Utils::unzipFileIntoDirectory(" + zipFileName_ + "," + destinationDirectory + ") exception " + e + " stacktrace: " + sw.toString());
|
2014-11-20 13:21:19 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2023-09-19 17:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int unzipChunksIntoDirectory(List<String> full_path_chunks, String destinationDirectory, char[] password, Log log) {
|
|
|
|
|
try {
|
|
|
|
|
// STEP 1: assemble the chunks into an actual zip (in RAM)
|
|
|
|
|
ByteArrayOutputStream unzippedData = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
for (String full_path_chunk: full_path_chunks) {
|
|
|
|
|
byte[] data = Files.readAllBytes(Paths.get(full_path_chunk));
|
|
|
|
|
|
|
|
|
|
unzippedData.write(data);
|
|
|
|
|
}
|
|
|
|
|
byte[] full_data = unzippedData.toByteArray();
|
|
|
|
|
|
|
|
|
|
// STEP 2: unzip the zip like before
|
|
|
|
|
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(full_data));
|
|
|
|
|
if (password != null) {
|
|
|
|
|
zipInputStream.setPassword(password);
|
|
|
|
|
}
|
|
|
|
|
LocalFileHeader fileHeader = null;
|
|
|
|
|
while ((fileHeader = zipInputStream.getNextEntry()) != null) {
|
|
|
|
|
String outFilePath = destinationDirectory + File.separator + fileHeader.getFileName();
|
|
|
|
|
File outFile = new File(outFilePath);
|
|
|
|
|
|
|
|
|
|
//Checks if the file is a directory
|
|
|
|
|
if (fileHeader.isDirectory()) {
|
|
|
|
|
outFile.mkdirs();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File parentDir = outFile.getParentFile();
|
|
|
|
|
if (parentDir.exists() == false) {
|
|
|
|
|
parentDir.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileOutputStream os = new FileOutputStream(outFile);
|
|
|
|
|
|
|
|
|
|
int readLen = -1;
|
|
|
|
|
byte[] buff = new byte[1024];
|
|
|
|
|
|
|
|
|
|
//Loop until End of File and write the contents to the output stream
|
|
|
|
|
while ((readLen = zipInputStream.read(buff)) != -1) {
|
|
|
|
|
os.write(buff, 0, readLen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.close();
|
|
|
|
|
}
|
|
|
|
|
zipInputStream.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
e.printStackTrace(pw);
|
|
|
|
|
log.debug("Utils::unzipChunksIntoDirectory exception " + e + " stacktrace: " + sw.toString());
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String md5(String path_of_file_) {
|
2023-01-04 17:08:35 +01:00
|
|
|
Md5 md5 = new Md5();
|
|
|
|
|
return md5.get(path_of_file_);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-29 16:36:12 +00:00
|
|
|
public static String convertBinaryToHex(byte[] bytes) {
|
|
|
|
|
StringBuilder hexStringBuilder = new StringBuilder();
|
|
|
|
|
for (byte aByte : bytes) {
|
|
|
|
|
char[] hex = new char[2];
|
|
|
|
|
hex[0] = Character.forDigit((aByte >> 4) & 0xF, 16);
|
|
|
|
|
hex[1] = Character.forDigit((aByte & 0xF), 16);
|
|
|
|
|
hexStringBuilder.append(new String(hex));
|
|
|
|
|
}
|
|
|
|
|
return hexStringBuilder.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 13:21:19 +00:00
|
|
|
public static double lastModificationTime(File directory_) {
|
|
|
|
|
double max = 0.0;
|
|
|
|
|
if (directory_.isDirectory()) {
|
|
|
|
|
File[] list = directory_.listFiles();
|
|
|
|
|
if (list != null) {
|
2015-01-25 18:36:39 +00:00
|
|
|
for (File aFile : list) {
|
|
|
|
|
double max1 = lastModificationTime(aFile);
|
2014-11-20 13:21:19 +00:00
|
|
|
if (max1 > max) {
|
|
|
|
|
max = max1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (directory_.isFile()) {
|
|
|
|
|
return directory_.lastModified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServerCode statusIsOK(Document document_, String rootname_) {
|
2014-12-16 21:17:34 +00:00
|
|
|
if (document_ == null) {
|
|
|
|
|
return Error.ServerCode.UNKNOWN;
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
NodeList ns = document_.getElementsByTagName(rootname_);
|
|
|
|
|
if (ns.getLength() == 0) {
|
|
|
|
|
return Error.ServerCode.ERROR_NO_ROOT;
|
|
|
|
|
}
|
|
|
|
|
Element a_node = (Element) ns.item(0);
|
|
|
|
|
if (a_node.hasAttribute("status")) {
|
|
|
|
|
return Error.ServerCode.fromInt(Integer.parseInt(a_node.getAttribute("status")));
|
|
|
|
|
}
|
|
|
|
|
return Error.ServerCode.UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will recursively delete a directory
|
|
|
|
|
*/
|
|
|
|
|
public static void delete(File file) {
|
|
|
|
|
if (file == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (file.isDirectory()) {
|
2021-05-11 15:36:59 +00:00
|
|
|
String[] files = file.list();
|
2014-11-20 13:21:19 +00:00
|
|
|
if (files != null) {
|
|
|
|
|
if (files.length != 0) {
|
|
|
|
|
for (String temp : files) {
|
|
|
|
|
File fileDelete = new File(file, temp);
|
|
|
|
|
delete(fileDelete);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file.delete();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 13:53:18 +01:00
|
|
|
/**
|
|
|
|
|
* Parse a number string to a number.
|
|
|
|
|
* Input can be as "32", "10k", "100K", "100G", "1.3G", "0.4T"
|
|
|
|
|
*/
|
2014-11-20 13:21:19 +00:00
|
|
|
public static long parseNumber(String in) {
|
|
|
|
|
in = in.trim();
|
|
|
|
|
in = in.replaceAll(",", ".");
|
|
|
|
|
try {
|
|
|
|
|
return Long.parseLong(in);
|
|
|
|
|
}
|
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
|
}
|
|
|
|
|
final Matcher m = Pattern.compile("([\\d.,]+)\\s*(\\w)").matcher(in);
|
|
|
|
|
m.find();
|
|
|
|
|
int scale = 1;
|
|
|
|
|
switch (m.group(2).charAt(0)) {
|
2019-02-20 13:53:18 +01:00
|
|
|
case 'T':
|
|
|
|
|
case 't':
|
|
|
|
|
scale = 1000 * 1000 * 1000 * 1000;
|
|
|
|
|
break;
|
2014-11-20 13:21:19 +00:00
|
|
|
case 'G':
|
|
|
|
|
case 'g':
|
2019-02-20 13:53:18 +01:00
|
|
|
scale = 1000 * 1000 * 1000;
|
|
|
|
|
break;
|
2014-11-20 13:21:19 +00:00
|
|
|
case 'M':
|
|
|
|
|
case 'm':
|
2019-02-20 13:53:18 +01:00
|
|
|
scale = 1000 * 1000;
|
|
|
|
|
break;
|
2014-11-20 13:21:19 +00:00
|
|
|
case 'K':
|
2019-02-20 13:53:18 +01:00
|
|
|
case 'k':
|
|
|
|
|
scale = 1000;
|
2014-11-20 13:21:19 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return Math.round(Double.parseDouble(m.group(1)) * scale);
|
|
|
|
|
}
|
2015-01-28 00:46:08 +00:00
|
|
|
|
|
|
|
|
public static String humanDuration(Date date) {
|
|
|
|
|
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
|
|
|
|
|
calendar.setTime(date);
|
|
|
|
|
|
|
|
|
|
int hours = (calendar.get(Calendar.DAY_OF_MONTH) - 1) * 24 + calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
int minutes = calendar.get(Calendar.MINUTE);
|
|
|
|
|
int seconds = calendar.get(Calendar.SECOND);
|
|
|
|
|
|
|
|
|
|
String output = "";
|
|
|
|
|
if (hours > 0) {
|
2020-04-12 21:18:35 +10:00
|
|
|
output += hours + "h ";
|
2015-01-28 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
if (minutes > 0) {
|
2020-04-12 21:18:35 +10:00
|
|
|
output += minutes + "min ";
|
2015-01-28 00:46:08 +00:00
|
|
|
}
|
|
|
|
|
if (seconds > 0) {
|
|
|
|
|
output += seconds + "s";
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2016-06-25 13:45:09 +02:00
|
|
|
|
2021-10-01 16:00:46 +02:00
|
|
|
public static boolean noFreeSpaceOnDisk(String destination_, Log log) {
|
2016-06-25 13:45:09 +02:00
|
|
|
try {
|
|
|
|
|
File file = new File(destination_);
|
2023-01-09 11:29:40 +01:00
|
|
|
for (int i = 0; i < 5; i++) { //We poll repeatedly because getUsableSpace() might just return 0 on busy disk IO
|
2021-10-01 16:00:46 +02:00
|
|
|
long space = file.getUsableSpace();
|
|
|
|
|
if (space > 512 * 1024) { // at least the same amount as Server.HTTPGetFile
|
2021-08-21 21:53:11 +00:00
|
|
|
return false; // If we are not "full", we are done, no need for additional polling
|
2023-01-09 11:29:40 +01:00
|
|
|
} else if (i < 4) {
|
|
|
|
|
long time = (long) (
|
|
|
|
|
Math.random() * (100 - 50 + 1) + 50 + //Wait between 50 and 100 milliseconds,
|
|
|
|
|
(i * 500) //add 500 ms on each failed poll
|
|
|
|
|
);
|
2021-10-01 16:00:46 +02:00
|
|
|
log.debug("Utils::Not enough free disk space(" + space + ") encountered on try " + i + ", waiting " + time + "ms");
|
|
|
|
|
Thread.sleep(time);
|
2021-08-21 21:53:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2016-06-25 13:45:09 +02:00
|
|
|
}
|
2021-08-21 21:53:11 +00:00
|
|
|
catch (SecurityException | InterruptedException e) {
|
2016-06-25 13:45:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-07-07 15:37:15 +02:00
|
|
|
|
|
|
|
|
public static String findMimeType(String file) throws IOException {
|
|
|
|
|
String mimeType = Files.probeContentType(Paths.get(file));
|
|
|
|
|
if (mimeType == null) {
|
|
|
|
|
InputStream stream = new BufferedInputStream(new FileInputStream(file));
|
|
|
|
|
mimeType = URLConnection.guessContentTypeFromStream(stream);
|
|
|
|
|
}
|
|
|
|
|
if (mimeType == null) {
|
|
|
|
|
mimeType = URLConnection.guessContentTypeFromName(file);
|
|
|
|
|
}
|
2021-11-25 12:59:19 +00:00
|
|
|
|
2023-01-06 14:53:06 +00:00
|
|
|
if (mimeType == null || (mimeType.equals("image/aces") && file.toLowerCase().endsWith(".exr"))) {
|
|
|
|
|
try {
|
|
|
|
|
String extension = file.substring(file.lastIndexOf('.'));
|
|
|
|
|
mimeType = mimeTypes.get(extension);
|
|
|
|
|
}
|
|
|
|
|
catch (IndexOutOfBoundsException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2022-07-30 09:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:37:15 +02:00
|
|
|
return mimeType;
|
|
|
|
|
}
|
2020-09-12 10:00:24 +10:00
|
|
|
|
|
|
|
|
public static String formatDataConsumption(long bytes) {
|
|
|
|
|
float divider = 0;
|
|
|
|
|
String suffix = "";
|
|
|
|
|
|
|
|
|
|
if (bytes > 1099511627776f) { // 1TB
|
|
|
|
|
divider = 1099511627776f;
|
|
|
|
|
suffix = "TB";
|
|
|
|
|
}
|
|
|
|
|
else if (bytes > 1073741824) { // 1GB
|
|
|
|
|
divider = 1073741824;
|
|
|
|
|
suffix = "GB";
|
|
|
|
|
}
|
|
|
|
|
else { // 1MB
|
|
|
|
|
divider = 1048576;
|
|
|
|
|
suffix = "MB";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.format("%.2f%s", (bytes / divider), suffix);
|
|
|
|
|
}
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|