use of org.matrix.olm.OlmException in project matrix-android-sdk by matrix-org.
the class MediaScanRestClient method scanEncryptedFile.
/**
* Scan an encrypted file.
*
* @param encryptedMediaScanBody the encryption information required to decrypt the content before scanning it.
* @param callback on success callback containing a MediaScanResult object
*/
public void scanEncryptedFile(final EncryptedMediaScanBody encryptedMediaScanBody, final ApiCallback<MediaScanResult> callback) {
// Encrypt encryptedMediaScanBody if the server support it
getServerPublicKey(new SimpleApiCallback<String>(callback) {
@Override
public void onSuccess(String serverPublicKey) {
Call<MediaScanResult> request;
// Encrypt the data, if antivirus server supports it
if (!TextUtils.isEmpty(serverPublicKey)) {
try {
OlmPkEncryption olmPkEncryption = new OlmPkEncryption();
olmPkEncryption.setRecipientKey(serverPublicKey);
String data = JsonUtility.getCanonicalizedJsonString(encryptedMediaScanBody);
OlmPkMessage message = olmPkEncryption.encrypt(data);
EncryptedMediaScanEncryptedBody encryptedMediaScanEncryptedBody = new EncryptedMediaScanEncryptedBody();
encryptedMediaScanEncryptedBody.encryptedBodyFileInfo = new EncryptedBodyFileInfo(message);
request = mApi.scanEncrypted(encryptedMediaScanEncryptedBody);
} catch (OlmException e) {
// should not happen. Send the error to the caller
request = null;
callback.onUnexpectedError(e);
}
} else {
// No public key on this server, do not encrypt data
request = mApi.scanEncrypted(encryptedMediaScanBody);
}
if (request != null) {
request.enqueue(new RestAdapterCallback<>("scanEncryptedFile", null, new SimpleApiCallback<MediaScanResult>(callback) {
@Override
public void onSuccess(MediaScanResult scanResult) {
callback.onSuccess(scanResult);
}
@Override
public void onMatrixError(MatrixError e) {
// Check whether the provided encrypted_body could not be decrypted.
if (e.mStatus == HttpURLConnection.HTTP_FORBIDDEN) {
MediaScanError mcsError;
try {
mcsError = JsonUtils.getGson(false).fromJson(e.mErrorBodyAsString, MediaScanError.class);
} catch (Exception exc) {
mcsError = null;
}
if (mcsError != null && MediaScanError.MCS_BAD_DECRYPTION.equals(mcsError.reason)) {
// The client should request again the public key of the server.
resetServerPublicKey();
}
}
super.onMatrixError(e);
}
}, null));
}
}
});
}
Aggregations