Commit 09e88ddd authored by Tiago Cunha's avatar Tiago Cunha Committed by GitHub

Merge pull request #245 from RocketChat/feature/google-storage-support

GCP uploading
parents 4f259cc3 cf4e33d5
......@@ -21,16 +21,14 @@ public class FileUploadingHelper extends MethodCallHelper {
super(realmHelper, ddpClientRef);
}
public Task<JSONObject> uploadRequest(String filename, long filesize, String mimeType,
String roomId) {
return call("slingshot/uploadRequest", TIMEOUT_MS, () -> new JSONArray()
.put("rocketchat-uploads")
.put(new JSONObject()
.put("name", filename)
.put("size", filesize)
.put("type", mimeType))
.put(new JSONObject().put("rid", roomId)))
.onSuccessTask(CONVERT_TO_JSON_OBJECT);
public Task<JSONObject> uploadS3Request(String filename, long filesize, String mimeType,
String roomId) {
return uploadRequest("rocketchat-uploads", filename, filesize, mimeType, roomId);
}
public Task<JSONObject> uploadGoogleRequest(String filename, long filesize, String mimeType,
String roomId) {
return uploadRequest("rocketchat-uploads-gs", filename, filesize, mimeType, roomId);
}
public Task<Void> sendFileMessage(String roomId, String storageType, JSONObject fileObj) {
......@@ -59,4 +57,17 @@ public class FileUploadingHelper extends MethodCallHelper {
.put(token)
).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;
import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber;
import chat.rocket.android.service.ddp.base.UserDataSubscriber;
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.GcmPushRegistrationObserver;
import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
......@@ -50,7 +50,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
GetUsersOfRoomsProcedureObserver.class,
NewMessageObserver.class,
CurrentUserObserver.class,
FileUploadingToS3Observer.class,
FileUploadingToUrlObserver.class,
FileUploadingWithUfsObserver.class,
PushSettingsObserver.class,
GcmPushRegistrationObserver.class
......
......@@ -31,11 +31,11 @@ import okio.Source;
/**
* execute file uploading and requesting sendMessage with attachment.
*/
public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploading> {
public class FileUploadingToUrlObserver extends AbstractModelObserver<FileUploading> {
private FileUploadingHelper methodCall;
public FileUploadingToS3Observer(Context context, String hostname,
RealmHelper realmHelper, DDPClientRef ddpClientRef) {
public FileUploadingToUrlObserver(Context context, String hostname,
RealmHelper realmHelper, DDPClientRef ddpClientRef) {
super(context, hostname, realmHelper, ddpClientRef);
methodCall = new FileUploadingHelper(realmHelper, ddpClientRef);
......@@ -43,7 +43,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
// resume pending operations.
RealmResults<FileUploading> pendingUploadRequests = realm.where(FileUploading.class)
.equalTo(FileUploading.SYNC_STATE, SyncState.SYNCING)
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll();
for (FileUploading req : pendingUploadRequests) {
req.setSyncState(SyncState.NOT_SYNCED);
......@@ -56,7 +60,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
.or()
.equalTo(FileUploading.SYNC_STATE, SyncState.FAILED)
.endGroup()
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll().deleteAllFromRealm();
return null;
}).continueWith(new LogIfError());
......@@ -66,7 +74,11 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
public RealmResults<FileUploading> queryItems(Realm realm) {
return realm.where(FileUploading.class)
.equalTo(FileUploading.SYNC_STATE, SyncState.NOT_SYNCED)
.beginGroup()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_S3)
.or()
.equalTo(FileUploading.STORAGE_TYPE, FileUploading.STORAGE_TYPE_GOOGLE)
.endGroup()
.findAll();
}
......@@ -91,13 +103,20 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
final long filesize = fileUploading.getFilesize();
final String mimeType = fileUploading.getMimeType();
final Uri fileUri = Uri.parse(fileUploading.getUri());
final String storageType = fileUploading.getStorageType();
realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject()
.put(FileUploading.ID, uplId)
.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 -> {
final JSONObject info = task.getResult();
final String uploadUrl = info.getString("upload");
......@@ -156,7 +175,9 @@ public class FileUploadingToS3Observer extends AbstractModelObserver<FileUploadi
}
}).onSuccessTask(task -> {
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("type", mimeType)
.put("size", filesize)
......
......@@ -23,6 +23,7 @@ public class FileUploading extends RealmObject {
public static final String STORAGE_TYPE_S3 = "AmazonS3";
public static final String STORAGE_TYPE_GRID_FS = "GridFS";
public static final String STORAGE_TYPE_FILE_SYSTEM = "FileSystem";
public static final String STORAGE_TYPE_GOOGLE = "GoogleCloudStorage";
@PrimaryKey private String uplId;
private int syncstate;
......
......@@ -158,9 +158,13 @@ public class RocketChatMessageAttachmentsLayout extends LinearLayout {
titleView.setOnClickListener(new OnClickListener() {
@Override
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);
view.getContext().startActivity(intent);
final Context context = view.getContext();
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
}
}
});
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