|
| 1 | +package com.annimon.ownlang.modules.downloader; |
| 2 | + |
| 3 | +import com.annimon.ownlang.Console; |
| 4 | +import com.annimon.ownlang.lib.Arguments; |
| 5 | +import com.annimon.ownlang.lib.Function; |
| 6 | +import com.annimon.ownlang.lib.FunctionValue; |
| 7 | +import com.annimon.ownlang.lib.Functions; |
| 8 | +import com.annimon.ownlang.lib.NumberValue; |
| 9 | +import com.annimon.ownlang.lib.Types; |
| 10 | +import com.annimon.ownlang.lib.Value; |
| 11 | +import com.annimon.ownlang.modules.Module; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.net.HttpURLConnection; |
| 17 | +import java.net.URL; |
| 18 | + |
| 19 | +public final class downloader implements Module { |
| 20 | + |
| 21 | + @Override |
| 22 | + public void init() { |
| 23 | + Functions.set("getContentLength", this::getContentLength); |
| 24 | + Functions.set("downloader", this::downloader); |
| 25 | + } |
| 26 | + |
| 27 | + private Value getContentLength(Value... args) { |
| 28 | + Arguments.check(1, args.length); |
| 29 | + return NumberValue.of(getContentLength(args[0].asString())); |
| 30 | + } |
| 31 | + |
| 32 | + private Value downloader(Value... args) { |
| 33 | + Arguments.checkRange(2, 4, args.length); |
| 34 | + final String downloadUrl = args[0].asString(); |
| 35 | + final String filePath = args[1].asString(); |
| 36 | + final Function progressCallback; |
| 37 | + final int contentLength; |
| 38 | + if ( (args.length >= 3) && (args[2].type() == Types.FUNCTION) ) { |
| 39 | + progressCallback = ((FunctionValue) args[2]).getValue(); |
| 40 | + // For showing progress we need to get content length |
| 41 | + contentLength = getContentLength(downloadUrl); |
| 42 | + } else { |
| 43 | + progressCallback = null; |
| 44 | + contentLength = -1; |
| 45 | + } |
| 46 | + final int bufferSize = (args.length == 4) ? Math.max(1024, args[3].asInt()) : 16384; |
| 47 | + |
| 48 | + final NumberValue contentLengthBoxed = NumberValue.of(contentLength); |
| 49 | + final boolean calculateProgressEnabled = contentLength > 0 && progressCallback != null; |
| 50 | + if (calculateProgressEnabled) { |
| 51 | + progressCallback.execute( |
| 52 | + NumberValue.ZERO /*%*/, |
| 53 | + NumberValue.ZERO /*bytes downloaded*/, |
| 54 | + contentLengthBoxed /*file size*/); |
| 55 | + } |
| 56 | + |
| 57 | + try (InputStream is = new URL(downloadUrl).openStream(); |
| 58 | + OutputStream os = new FileOutputStream(Console.fileInstance(filePath))) { |
| 59 | + int downloaded = 0; |
| 60 | + final byte[] buffer = new byte[bufferSize]; |
| 61 | + int readed; |
| 62 | + while ((readed = is.read(buffer, 0, bufferSize)) != -1) { |
| 63 | + os.write(buffer, 0, readed); |
| 64 | + downloaded += readed; |
| 65 | + if (calculateProgressEnabled) { |
| 66 | + final int percent = downloaded * 100 / contentLength; |
| 67 | + progressCallback.execute( |
| 68 | + NumberValue.of(percent), |
| 69 | + NumberValue.of(downloaded), |
| 70 | + contentLengthBoxed); |
| 71 | + } |
| 72 | + } |
| 73 | + } catch (IOException ioe) { |
| 74 | + return NumberValue.ZERO; |
| 75 | + } finally { |
| 76 | + if (progressCallback != null) { |
| 77 | + progressCallback.execute(NumberValue.of(100), contentLengthBoxed, contentLengthBoxed); |
| 78 | + } |
| 79 | + } |
| 80 | + return NumberValue.ONE; |
| 81 | + } |
| 82 | + |
| 83 | + private static int getContentLength(String url) { |
| 84 | + HttpURLConnection connection = null; |
| 85 | + try { |
| 86 | + connection = (HttpURLConnection) new URL(url).openConnection(); |
| 87 | + connection.setRequestMethod("HEAD"); |
| 88 | + connection.connect(); |
| 89 | + return connection.getContentLength(); |
| 90 | + } catch (IOException ioe) { |
| 91 | + return -1; |
| 92 | + } finally { |
| 93 | + if (connection != null) { |
| 94 | + connection.disconnect(); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments