Add method for downloading a file from a mailbox

This commit is contained in:
Torsten Grote
2022-01-21 11:59:25 -03:00
parent 76599a8d04
commit 0cb2dcf6b7
3 changed files with 75 additions and 0 deletions

View File

@@ -77,6 +77,17 @@ interface MailboxApi {
List<MailboxFile> getFiles(MailboxProperties properties, String folderId)
throws IOException, ApiException;
/**
* Used by owner and contacts to retrieve a file.
* <p>
* Returns 200 OK if successful with the files' raw bytes
* in the response body.
*
* @param file the empty file the response bytes will be written into.
*/
void getFile(MailboxProperties properties, String folderId,
String fileId, File file) throws IOException, ApiException;
@Immutable
@JsonSerialize
class MailboxContact {

View File

@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@@ -28,6 +29,7 @@ import okhttp3.ResponseBody;
import static com.fasterxml.jackson.databind.MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES;
import static java.util.Objects.requireNonNull;
import static okhttp3.internal.Util.EMPTY_REQUEST;
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
import static org.briarproject.bramble.util.StringUtils.fromHexString;
@NotNullByDefault
@@ -206,6 +208,19 @@ class MailboxApiImpl implements MailboxApi {
}
}
@Override
public void getFile(MailboxProperties properties, String folderId,
String fileId, File file) throws IOException, ApiException {
String path = "/files/" + folderId + "/" + fileId;
Response response = sendGetRequest(properties, path);
if (response.code() != 200) throw new ApiException();
ResponseBody body = response.body();
if (body == null) throw new ApiException();
FileOutputStream outputStream = new FileOutputStream(file);
copyAndClose(body.byteStream(), outputStream);
}
/* Helper Functions */
private Response sendGetRequest(MailboxProperties properties, String path)