@@ -36,6 +36,8 @@ import ru.soune.nocopy.util.FileUtil;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
import java.nio.file.attribute.PosixFilePermission;
|
import java.nio.file.attribute.PosixFilePermission;
|
||||||
@@ -598,7 +600,165 @@ public class FileUploadServiceImpl implements FileUploadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public Path downloadImageFromUrl(String urlString) throws IOException {
|
||||||
|
// String[] urlVariants = buildUrlVariants(urlString);
|
||||||
|
//
|
||||||
|
// IOException lastException = null;
|
||||||
|
//
|
||||||
|
// for (String variantUrl : urlVariants) {
|
||||||
|
// try {
|
||||||
|
// Path result = attemptDownload(variantUrl);
|
||||||
|
// if (result != null) {
|
||||||
|
// log.info("Success download: {} -> {}", urlString, variantUrl);
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// lastException = e;
|
||||||
|
// log.warn("Failed download {}: {}", variantUrl, e.getMessage());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// throw new IOException("Failed to download from all variants: " + urlString, lastException);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private String[] buildUrlVariants(String originalUrl) {
|
||||||
|
// List<String> variants = new ArrayList<>();
|
||||||
|
//
|
||||||
|
// variants.add(originalUrl);
|
||||||
|
//
|
||||||
|
// if (originalUrl.contains("i.ytimg.com") || originalUrl.contains("youtube.com")) {
|
||||||
|
// try {
|
||||||
|
// URL url = new URL(originalUrl);
|
||||||
|
// String path = url.getPath();
|
||||||
|
//
|
||||||
|
// if ("https".equals(url.getProtocol())) {
|
||||||
|
// variants.add("http://i.ytimg.com" + path);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// String[][] alternativeHosts = {
|
||||||
|
// {"i.ytimg.com", "i3.ytimg.com"},
|
||||||
|
// {"i.ytimg.com", "img.youtube.com"},
|
||||||
|
// {"i.ytimg.com", "yt3.ggpht.com"}
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// for (String[] hosts : alternativeHosts) {
|
||||||
|
// if (originalUrl.contains(hosts[0])) {
|
||||||
|
// variants.add(originalUrl.replace(hosts[0], hosts[1]));
|
||||||
|
// variants.add("http://" + hosts[1] + path);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// } catch (MalformedURLException e) {
|
||||||
|
// log.warn("Not valid URL: {}", originalUrl);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return variants.toArray(new String[0]);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private Path attemptDownload(String urlString) throws IOException {
|
||||||
|
// int maxRetries = 2;
|
||||||
|
// int connectTimeout = 10000;
|
||||||
|
// int readTimeout = 10000;
|
||||||
|
//
|
||||||
|
// IOException lastException = null;
|
||||||
|
//
|
||||||
|
// for (int attempt = 0; attempt < maxRetries; attempt++) {
|
||||||
|
// Path tempFile = null;
|
||||||
|
// HttpURLConnection connection = null;
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// URL url = new URL(urlString);
|
||||||
|
//
|
||||||
|
// String extension = getFileExtension(url.getPath());
|
||||||
|
//
|
||||||
|
// tempFile = Files.createTempFile("upload_check_", extension);
|
||||||
|
//
|
||||||
|
// connection = (HttpURLConnection) url.openConnection();
|
||||||
|
// connection.setRequestProperty("User-Agent",
|
||||||
|
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
||||||
|
// connection.setConnectTimeout(connectTimeout);
|
||||||
|
// connection.setReadTimeout(readTimeout);
|
||||||
|
// connection.setInstanceFollowRedirects(true);
|
||||||
|
//
|
||||||
|
// int responseCode = connection.getResponseCode();
|
||||||
|
// log.debug("{} -> Response code: {} (попытка {})", urlString, responseCode, attempt + 1);
|
||||||
|
//
|
||||||
|
// if (responseCode != 200) {
|
||||||
|
// throw new IOException("Bad response code: " + responseCode);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// try (InputStream in = connection.getInputStream()) {
|
||||||
|
// Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// long size = Files.size(tempFile);
|
||||||
|
// log.debug("Downloaded: {} -> size: {} byte", urlString, size);
|
||||||
|
//
|
||||||
|
// if (size == 0) {
|
||||||
|
// throw new IOException("Downloaded empty file");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return tempFile;
|
||||||
|
//
|
||||||
|
// } catch (SocketTimeoutException e) {
|
||||||
|
// lastException = e;
|
||||||
|
// log.warn("Timeout {} (try {}/{})", urlString, attempt + 1, maxRetries);
|
||||||
|
//
|
||||||
|
// connectTimeout *= 2;
|
||||||
|
// readTimeout *= 2;
|
||||||
|
//
|
||||||
|
// if (attempt < maxRetries - 1) {
|
||||||
|
// try {
|
||||||
|
// Thread.sleep(1000 * (attempt + 1));
|
||||||
|
// } catch (InterruptedException ie) {
|
||||||
|
// Thread.currentThread().interrupt();
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// lastException = e;
|
||||||
|
// if (!(e instanceof SocketTimeoutException)) {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// } finally {
|
||||||
|
// if (connection != null) {
|
||||||
|
// connection.disconnect();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (tempFile != null && Files.exists(tempFile)) {
|
||||||
|
// try {
|
||||||
|
// Files.deleteIfExists(tempFile);
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// log.warn("Temp file not found: {}", tempFile);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// throw new IOException("Not download " + maxRetries + " tries: " + urlString, lastException);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private String getFileExtension(String path) {
|
||||||
|
// String extension = ".tmp";
|
||||||
|
// int lastDot = path.lastIndexOf('.');
|
||||||
|
// if (lastDot > 0) {
|
||||||
|
// String ext = path.substring(lastDot).toLowerCase();
|
||||||
|
// if (ext.matches("\\.(jpg|jpeg|png|gif|bmp|webp)")) {
|
||||||
|
// extension = ext;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return extension;
|
||||||
|
// }
|
||||||
|
|
||||||
public Path downloadImageFromUrl(String urlString) throws IOException {
|
public Path downloadImageFromUrl(String urlString) throws IOException {
|
||||||
|
if (urlString.startsWith("https://i.ytimg.com")) {
|
||||||
|
urlString = urlString.replace("https://", "http://");
|
||||||
|
log.info("Change blocked domen: {}", urlString);
|
||||||
|
}
|
||||||
|
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
|
|
||||||
String extension = ".tmp";
|
String extension = ".tmp";
|
||||||
|
|||||||
Reference in New Issue
Block a user