|
1 | 1 | package com.convertapi; |
2 | 2 |
|
3 | 3 | import com.convertapi.model.ConversionResponse; |
| 4 | +import com.convertapi.model.RemoteUploadResponse; |
4 | 5 | import com.convertapi.model.User; |
5 | 6 | import com.google.gson.Gson; |
6 | 7 | import okhttp3.HttpUrl; |
@@ -107,29 +108,89 @@ public static User getUser(Config config) { |
107 | 108 | } |
108 | 109 |
|
109 | 110 | @SuppressWarnings("unused") |
110 | | - public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat) throws IOException { |
111 | | - return convert(fromFile, toFormat, Config.defaults().getSecret()); |
| 111 | + public static CompletableFuture<ConversionResult> convertFile(Path fromFile, String toFormat, Param... params) throws IOException { |
| 112 | + return convertFile(fromFile, toFormat, Config.defaults().getSecret(), params); |
112 | 113 | } |
113 | 114 |
|
114 | | - public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret) throws IOException { |
115 | | - return convert(getFileExtension(fromFile), toFormat, new Param[]{new Param("file", fromFile)}, Config.defaults(secret)); |
| 115 | + public static CompletableFuture<ConversionResult> convertFile(Path fromFile, String toFormat, String secret, Param... params) throws IOException { |
| 116 | + Param[] fileParam = new Param[]{new Param("file", fromFile)}; |
| 117 | + return convert(getFileExtension(fromFile), toFormat, Param.concat(fileParam, params), Config.defaults(secret)); |
116 | 118 | } |
117 | 119 |
|
118 | 120 | @SuppressWarnings("unused") |
119 | | - public static void convert(String fromPathToFile, String toPathToFile) { |
120 | | - convert(fromPathToFile, toPathToFile, Config.defaults().getSecret()); |
| 121 | + public static void convertFile(String fromPathToFile, String toPathToFile) { |
| 122 | + convertFile(fromPathToFile, toPathToFile, Config.defaults().getSecret()); |
121 | 123 | } |
122 | 124 |
|
123 | | - public static void convert(String fromPathToFile, String toPathToFile, String secret) { |
| 125 | + public static void convertFile(String fromPathToFile, String toPathToFile, String secret, Param... params) { |
124 | 126 | try { |
125 | 127 | Path fromPath = Paths.get(fromPathToFile); |
126 | 128 | Path toPath = Paths.get(toPathToFile); |
127 | | - convert(fromPath, getFileExtension(toPath), secret).get().saveFile(toPath).get(); |
| 129 | + convertFile(fromPath, getFileExtension(toPath), secret, params).get().saveFile(toPath).get(); |
128 | 130 | } catch (IOException | ExecutionException | InterruptedException e) { |
129 | 131 | throw new RuntimeException(e); |
130 | 132 | } |
131 | 133 | } |
132 | 134 |
|
| 135 | + public static List<Path> convertFileToDir(String fromPathToFile, String toFormat, String outputDirectory, Param... params) { |
| 136 | + return convertFileToDir(fromPathToFile, toFormat, outputDirectory, Config.defaults().getSecret(), params); |
| 137 | + } |
| 138 | + |
| 139 | + public static List<Path> convertFileToDir(String fromPathToFile, String toFormat, String outputDirectory, String secret, Param... params) { |
| 140 | + try { |
| 141 | + Path fromPath = Paths.get(fromPathToFile); |
| 142 | + Path toPath = Paths.get(outputDirectory); |
| 143 | + return convertFile(fromPath, toFormat, secret, params).get().saveFilesSync(toPath); |
| 144 | + } catch (IOException | ExecutionException | InterruptedException e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static Path convertUrl(String url, String toPathToFile, Param... params) { |
| 150 | + return convertUrl(url, toPathToFile, Config.defaults().getSecret(), params); |
| 151 | + } |
| 152 | + |
| 153 | + public static Path convertUrl(String url, String toPathToFile, String secret, Param... params) { |
| 154 | + try { |
| 155 | + Path toPath = Paths.get(toPathToFile); |
| 156 | + Param[] urlParam = new Param[]{new Param("url", url)}; |
| 157 | + return convert("web", getFileExtension(toPath), Param.concat(urlParam, params), Config.defaults(secret)).get().saveFile(toPath).get(); |
| 158 | + } catch (ExecutionException | InterruptedException e) { |
| 159 | + throw new RuntimeException(e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public static Path convertRemoteFile(String url, String toPathToFile, Param... params) { |
| 164 | + return convertRemoteFile(url, toPathToFile, Config.defaults().getSecret(), params); |
| 165 | + } |
| 166 | + |
| 167 | + public static Path convertRemoteFile(String url, String toPathToFile, String secret, Param... params) { |
| 168 | + RemoteUploadResponse response = Http.remoteUpload(url, Config.defaults(secret)); |
| 169 | + try { |
| 170 | + Path toPath = Paths.get(toPathToFile); |
| 171 | + return convert(response.FileExt, getFileExtension(toPath), new Param[]{ |
| 172 | + new Param("file", response.FileId) |
| 173 | + }, Config.defaults(secret)).get().saveFile(toPath).get(); |
| 174 | + } catch (ExecutionException | InterruptedException e) { |
| 175 | + throw new RuntimeException(e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public static List<Path> convertRemoteFileToDir(String url, String toFormat, String outputDirectory, Param... params) { |
| 180 | + return convertRemoteFileToDir(url, toFormat, outputDirectory, Config.defaults().getSecret(), params); |
| 181 | + } |
| 182 | + |
| 183 | + public static List<Path> convertRemoteFileToDir(String url, String toFormat, String outputDirectory, String secret, Param... params) { |
| 184 | + RemoteUploadResponse response = Http.remoteUpload(url, Config.defaults(secret)); |
| 185 | + try { |
| 186 | + Path toPath = Paths.get(outputDirectory); |
| 187 | + Param[] fileParam = new Param[]{new Param("file", response.FileId)}; |
| 188 | + return convert(response.FileExt, toFormat, Param.concat(fileParam, params), Config.defaults(secret)).get().saveFilesSync(toPath); |
| 189 | + } catch (ExecutionException | InterruptedException e) { |
| 190 | + throw new RuntimeException(e); |
| 191 | + } |
| 192 | + } |
| 193 | + |
133 | 194 | private static String getFileExtension(Path path) { |
134 | 195 | String name = path.getFileName().toString(); |
135 | 196 | return name.substring(name.lastIndexOf(".") + 1); |
|
0 commit comments