Skip to content

Commit a6e8928

Browse files
committed
Delete source and result files from server
1 parent 9a5a70d commit a6e8928

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

src/com/convertapi/ConversionResult.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,24 @@ public List<Path> saveFilesSync(Path directory) {
6565
}
6666
}).collect(Collectors.toList());
6767
}
68+
69+
@SuppressWarnings("WeakerAccess")
70+
public List<CompletableFuture> delete() {
71+
List<CompletableFuture> futures = new ArrayList<>();
72+
for (int i = 0; i < response.Files.length; i++) {
73+
futures.add(getFile(i).delete());
74+
}
75+
return futures;
76+
}
77+
78+
@SuppressWarnings("WeakerAccess")
79+
public void deleteSync() {
80+
delete().forEach(d -> {
81+
try {
82+
d.get();
83+
} catch (InterruptedException | ExecutionException e) {
84+
throw new RuntimeException(e);
85+
}
86+
});
87+
}
6888
}

src/com/convertapi/ConversionResultFile.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public CompletableFuture<Path> saveFile(Path path) {
4848
}
4949
});
5050
}
51+
52+
@SuppressWarnings("WeakerAccess")
53+
public CompletableFuture delete() {
54+
return Http.requestDelete(getUrl());
55+
}
5156
}

src/com/convertapi/Http.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ static CompletableFuture<InputStream> requestGet(String url) {
4646
});
4747
}
4848

49+
static CompletableFuture<Void> requestDelete(String url) {
50+
return CompletableFuture.supplyAsync(() -> {
51+
Request request = getRequestBuilder().delete().url(url).build();
52+
try {
53+
getClient().newCall(request).execute();
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
57+
return null;
58+
});
59+
}
60+
4961
static Request.Builder getRequestBuilder() {
5062
return new Request.Builder()
5163
.header("User-Agent", "convertapi-java");

src/com/convertapi/Param.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public class Param {
1919
private final String name;
2020
private CompletableFuture<List<String>> value;
21+
private boolean isUploadedFile = false;
2122

2223
@SuppressWarnings("unused")
2324
private Param(String name) {
@@ -53,6 +54,7 @@ public Param(String name, byte[] value, String fileFormat, Config config) {
5354
String fileName = "getFile." + fileFormat;
5455
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
5556
this.value = upload(value, fileName, MediaType.parse(contentTypeString), config);
57+
isUploadedFile = true;
5658
}
5759

5860
public Param(String name, Path value) throws IOException {
@@ -64,6 +66,7 @@ public Param(String name, Path value, Config config) throws IOException {
6466
this(name);
6567
String contentTypeString = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(value.toFile());
6668
this.value = upload(Files.readAllBytes(value), value.getFileName().toString(), MediaType.parse(contentTypeString), config);
69+
isUploadedFile = true;
6770
}
6871

6972
@SuppressWarnings("unused")
@@ -94,6 +97,12 @@ public List<String> getValue() throws ExecutionException, InterruptedException {
9497
return this.value.get();
9598
}
9699

100+
public CompletableFuture<Void> delete() {
101+
return isUploadedFile
102+
? value.thenCompose(urls -> Http.requestDelete(urls.get(0)))
103+
: CompletableFuture.completedFuture(null);
104+
}
105+
97106
private static CompletableFuture<List<String>> upload(byte[] data, String fileName, MediaType fileContentType, Config config) {
98107
return CompletableFuture.supplyAsync(() -> {
99108
Request request = Http.getRequestBuilder()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.convertapi.examples;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import com.convertapi.Config;
11+
import com.convertapi.ConversionResult;
12+
import com.convertapi.ConvertApi;
13+
import com.convertapi.Param;
14+
15+
/**
16+
* Example of HTTP client setup to use HTTP proxy server.
17+
*/
18+
19+
public class Advanced {
20+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
21+
Config.setDefaultSecret("YOUR API SECRET"); //Get your secret at https://www.convertapi.com/a
22+
23+
// Advanced HTTP client setup
24+
Config.setDefaultHttpBuilder(builder -> {
25+
return builder
26+
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8888))) // Setting Proxy server
27+
.connectTimeout(3, TimeUnit.SECONDS); // Setting connect timeout
28+
// More settings can be tuned here
29+
});
30+
31+
// Conversion
32+
Param fileParam = new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx");
33+
System.out.println("Converting remote PPTX to PDF");
34+
CompletableFuture<ConversionResult> result = ConvertApi.convert("pptx", "pdf", new Param[]{fileParam});
35+
Path pdfFile = Paths.get(System.getProperty("java.io.tmpdir") + "/myfile.pdf");
36+
result.get().saveFile(pdfFile).get();
37+
38+
// Leaving no files on convertapi.com server
39+
System.out.println("Deleting source file from convertapi.com server");
40+
fileParam.delete().get();
41+
System.out.println("Deleting result files from convertapi.com server");
42+
result.get().deleteSync();
43+
44+
System.out.println("PDF file saved to: " + pdfFile.toString());
45+
}
46+
}

0 commit comments

Comments
 (0)