Files
sheepit-shadow-nabber/src/main/java/com/sheepit/client/Utils.java

337 lines
9.4 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2010-2014 Laurent CLOUET
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
*
* This program is free software; you can redistribute it and/or
* 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;
import java.io.BufferedInputStream;
2023-09-19 17:14:49 +00:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
2023-09-19 17:14:49 +00:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLConnection;
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;
import java.util.HashMap;
2023-09-19 17:14:49 +00:00
import java.util.List;
import java.util.Map;
2015-01-28 00:46:08 +00:00
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utils {
private static Map<String, String> mimeTypes = new HashMap<>();
static {
mimeTypes.put(".tga", "image/tga");
mimeTypes.put(".exr", "image/x-exr");
}
public static int unzipFileIntoDirectory(String zipFileName_, String destinationDirectory, char[] password, Log log) {
try {
ZipFile zipFile = new ZipFile(zipFileName_);
2021-05-11 15:36:59 +00:00
// unzipParameters.setIgnoreDateTimeAttributes(true);
if (password != null && zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
2021-05-11 15:36:59 +00:00
// zipFile.extractAll(destinationDirectory, unzipParameters);
zipFile.extractAll(destinationDirectory);
}
catch (ZipException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
log.debug("Utils::unzipFileIntoDirectory(" + zipFileName_ + "," + destinationDirectory + ") exception " + e + " stacktrace: " + sw.toString());
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;
}
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_);
}
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();
}
public static double lastModificationTime(File directory_) {
double max = 0.0;
if (directory_.isDirectory()) {
File[] list = directory_.listFiles();
if (list != null) {
for (File aFile : list) {
double max1 = lastModificationTime(aFile);
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;
}
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();
if (files != null) {
if (files.length != 0) {
for (String temp : files) {
File fileDelete = new File(file, temp);
delete(fileDelete);
}
}
}
}
file.delete();
}
/**
* Parse a number string to a number.
* Input can be as "32", "10k", "100K", "100G", "1.3G", "0.4T"
*/
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)) {
case 'T':
case 't':
scale = 1000 * 1000 * 1000 * 1000;
break;
case 'G':
case 'g':
scale = 1000 * 1000 * 1000;
break;
case 'M':
case 'm':
scale = 1000 * 1000;
break;
case 'K':
case 'k':
scale = 1000;
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) {
output += hours + "h ";
2015-01-28 00:46:08 +00:00
}
if (minutes > 0) {
output += minutes + "min ";
2015-01-28 00:46:08 +00:00
}
if (seconds > 0) {
output += seconds + "s";
}
return output;
}
public static boolean noFreeSpaceOnDisk(String destination_, Log log) {
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
long space = file.getUsableSpace();
if (space > 512 * 1024) { // at least the same amount as Server.HTTPGetFile
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
);
log.debug("Utils::Not enough free disk space(" + space + ") encountered on try " + i + ", waiting " + time + "ms");
Thread.sleep(time);
}
}
return true;
}
catch (SecurityException | InterruptedException e) {
}
return false;
}
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
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
}
return mimeType;
}
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);
}
}