Commit cf4e33d5 authored by Tiago Cunha's avatar Tiago Cunha

GCP uploading

parent 4f259cc3
...@@ -21,16 +21,14 @@ public class FileUploadingHelper extends MethodCallHelper { ...@@ -21,16 +21,14 @@ public class FileUploadingHelper extends MethodCallHelper {
super(realmHelper, ddpClientRef); super(realmHelper, ddpClientRef);
} }
public Task<JSONObject> uploadRequest(String filename, long filesize, String mimeType, public Task<JSONObject> uploadS3Request(String filename, long filesize, String mimeType,
String roomId) { String roomId) {
return call("slingshot/uploadRequest", TIMEOUT_MS, () -> new JSONArray() return uploadRequest("rocketchat-uploads", filename, filesize, mimeType, roomId);
.put("rocketchat-uploads") }
.put(new JSONObject()
.put("name", filename) public Task<JSONObject> uploadGoogleRequest(String filename, long filesize, String mimeType,
.put("size", filesize) String roomId) {
.put("type", mimeType)) return uploadRequest("rocketchat-uploads-gs", filename, filesize, mimeType, roomId);
.put(new JSONObject().put("rid", roomId)))
.onSuccessTask(CONVERT_TO_JSON_OBJECT);
} }
public Task<Void> sendFileMessage(String roomId, String storageType, JSONObject fileObj) { public Task<Void> sendFileMessage(String roomId, String storageType, JSONObject fileObj) {
...@@ -59,4 +57,17 @@ public class FileUploadingHelper extends MethodCallHelper { ...@@ -59,4 +57,17 @@ public class FileUploadingHelper extends MethodCallHelper {
.put(token) .put(token)
).onSuccessTask(CONVERT_TO_JSON_OBJECT); ).onSuccessTask(CONVERT_TO_JSON_OBJECT);
} }
private Task<JSONObject> uploadRequest(String uploadType, String filename,
long filesize, String mimeType,
String roomId) {
return call("slingshot/uploadRequest", TIMEOUT_MS, () -> new JSONArray()
.put(uploadType)
.put(new JSONObject()
.put("name", filename)
.put("size", filesize)
.put("type", mimeType))
.put(new JSONObject().put("rid", roomId)))
.onSuccessTask(CONVERT_TO_JSON_OBJECT);
}
} }
...@@ -22,7 +22,7 @@ import chat.rocket.android.service.ddp.base.ActiveUsersSubscriber; ...@@ -22,7 +22,7 @@ import chat.rocket.android.service.ddp.base.ActiveUsersSubscriber;
import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber; import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber;
import chat.rocket.android.service.ddp.base.UserDataSubscriber; import chat.rocket.android.service.ddp.base.UserDataSubscriber;
import chat.rocket.android.service.observer.CurrentUserObserver; import chat.rocket.android.service.observer.CurrentUserObserver;
import chat.rocket.android.service.observer.FileUploadingToS3Observer; import chat.rocket.android.service.observer.FileUploadingToUrlObserver;
import chat.rocket.android.service.observer.FileUploadingWithUfsObserver; import chat.rocket.android.service.observer.FileUploadingWithUfsObserver;
import chat.rocket.android.service.observer.GcmPushRegistrationObserver; import chat.rocket.android.service.observer.GcmPushRegistrationObserver;
import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver; import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
...@@ -50,7 +50,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -50,7 +50,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
GetUsersOfRoomsProcedureObserver.class, GetUsersOfRoomsProcedureObserver.class,
NewMessageObserver.class, NewMessageObserver.class,
CurrentUserObserver.class, CurrentUserObserver.class,
FileUploadingToS3Observer.class, FileUploadingToUrlObserver.class,
FileUploadingWithUfsObserver.class, FileUploadingWithUfsObserver.class,
PushSettingsObserver.class, PushSettingsObserver.class,
GcmPushRegistrationObserver.class GcmPushRegistrationObserver.class
......
...@@ -31,10 +31,10 @@ import okio.Source; ...@@ -31,10 +31,10 @@ import okio.Source;
/** /**
* execute file uploading and requesting sendMessage with attachment. * execute file uploading and requesting sendMessage with attachment.
*/ */
public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploading> { public class FileUploadingToUrlObserver extends AbstractModelObserver<FileUploading> {
private FileUploadingHelper methodCall; private FileUploadingHelper methodCall;
public FileUploadingToS3Observer(Context context, String hostname, public FileUploadingToUrlObserver(Context context, String hostname,
RealmHelper realmHelper, DDPClientRef ddpClientRef) { RealmHelper realmHelper, DDPClientRef ddpClientRef) {
super(context, hostname, realmHelper, ddpClientRef); super(context, hostname, realmHelper, ddpClientRef);
methodCall = new FileUploadingHelper(realmHelper, ddpClientRef); methodCall = new FileUploadingHelper(realmHelper, ddpClientRef);
...@@ -43,7 +43,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi ...@@ -43,7 +43,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
// resume pending operations. // resume pending operations.
RealmResults<FileUploading> pendingUploadRequests = realm.where(FileUploading.class) RealmResults<FileUploading> pendingUploadRequests = realm.where(FileUploading.class)
.equalTo(FileUploading.SYNC_STATE, SyncState.SYNCING) .equalTo(FileUploading.SYNC_STATE, SyncState.SYNCING)
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3) .equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll(); .findAll();
for (FileUploading req : pendingUploadRequests) { for (FileUploading req : pendingUploadRequests) {
req.setSyncState(SyncState.NOT_SYNCED); req.setSyncState(SyncState.NOT_SYNCED);
...@@ -56,7 +60,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi ...@@ -56,7 +60,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
.or() .or()
.equalTo(FileUploading.SYNC_STATE, SyncState.FAILED) .equalTo(FileUploading.SYNC_STATE, SyncState.FAILED)
.endGroup() .endGroup()
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3) .equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll().deleteAllFromRealm(); .findAll().deleteAllFromRealm();
return null; return null;
}).continueWith(new LogIfError()); }).continueWith(new LogIfError());
...@@ -66,7 +74,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi ...@@ -66,7 +74,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
public RealmResults<FileUploading> queryItems(Realm realm) { public RealmResults<FileUploading> queryItems(Realm realm) {
return realm.where(FileUploading.class) return realm.where(FileUploading.class)
.equalTo(FileUploading.SYNC_STATE, SyncState.NOT_SYNCED) .equalTo(FileUploading.SYNC_STATE, SyncState.NOT_SYNCED)
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3) .equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll(); .findAll();
} }
...@@ -91,13 +103,20 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi ...@@ -91,13 +103,20 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
final long filesize = fileUploading.getFilesize(); final long filesize = fileUploading.getFilesize();
final String mimeType = fileUploading.getMimeType(); final String mimeType = fileUploading.getMimeType();
final Uri fileUri = Uri.parse(fileUploading.getUri()); final Uri fileUri = Uri.parse(fileUploading.getUri());
final String storageType = fileUploading.getStorageType();
realmHelper.executeTransaction(realm -> realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject() realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject()
.put(FileUploading.ID, uplId) .put(FileUploading.ID, uplId)
.put(FileUploading.SYNC_STATE, SyncState.SYNCING) .put(FileUploading.SYNC_STATE, SyncState.SYNCING)
) )
).onSuccessTask(_task -> methodCall.uploadRequest(filename, filesize, mimeType, roomId) ).onSuccessTask(_task -> {
if (FileUploading.STORAGE_TYPE_GOOGLE.equals(storageType)) {
return methodCall.uploadGoogleRequest(filename, filesize, mimeType, roomId);
} else {
return methodCall.uploadS3Request(filename, filesize, mimeType, roomId);
}
}
).onSuccessTask(task -> { ).onSuccessTask(task -> {
final JSONObject info = task.getResult(); final JSONObject info = task.getResult();
final String uploadUrl = info.getString("upload"); final String uploadUrl = info.getString("upload");
...@@ -156,7 +175,9 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi ...@@ -156,7 +175,9 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
} }
}).onSuccessTask(task -> { }).onSuccessTask(task -> {
String downloadUrl = task.getResult(); String downloadUrl = task.getResult();
return methodCall.sendFileMessage(roomId, "s3", new JSONObject() String storage = FileUploading.STORAGE_TYPE_GOOGLE.equals(storageType) ? storageType : "s3";
return methodCall.sendFileMessage(roomId, storage, new JSONObject()
.put("_id", Uri.parse(downloadUrl).getLastPathSegment()) .put("_id", Uri.parse(downloadUrl).getLastPathSegment())
.put("type", mimeType) .put("type", mimeType)
.put("size", filesize) .put("size", filesize)
......
...@@ -23,6 +23,7 @@ public class FileUploading extends RealmObject { ...@@ -23,6 +23,7 @@ public class FileUploading extends RealmObject {
public static final String STORAGE_TYPE_S3 = "AmazonS3"; public static final String STORAGE_TYPE_S3 = "AmazonS3";
public static final String STORAGE_TYPE_GRID_FS = "GridFS"; public static final String STORAGE_TYPE_GRID_FS = "GridFS";
public static final String STORAGE_TYPE_FILE_SYSTEM = "FileSystem"; public static final String STORAGE_TYPE_FILE_SYSTEM = "FileSystem";
public static final String STORAGE_TYPE_GOOGLE = "GoogleCloudStorage";
@PrimaryKey private String uplId; @PrimaryKey private String uplId;
private int syncstate; private int syncstate;
......
...@@ -158,9 +158,13 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout { ...@@ -158,9 +158,13 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
titleView.setOnClickListener(new OnClickListener() { titleView.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
final Context context = view.getContext();
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
} }
}); });
TextViewCompat.setTextAppearance(titleView, TextViewCompat.setTextAppearance(titleView,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment