mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Add method for downloading a file from a mailbox
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user