Commit 9cf77f45 authored by Yusuke Iwaki's avatar Yusuke Iwaki

upload to FileSystem.

parent 2c65f620
...@@ -39,21 +39,21 @@ public class FileUploadingHelper extends MethodCallHelper { ...@@ -39,21 +39,21 @@ public class FileUploadingHelper extends MethodCallHelper {
.onSuccessTask(CONVERT_TO_JSON_OBJECT); .onSuccessTask(CONVERT_TO_JSON_OBJECT);
} }
public Task<JSONObject> ufsCreate(String filename, long filesize, String mimeType, public Task<JSONObject> ufsCreate(String filename, long filesize, String mimeType, String store,
String roomId) { String roomId) {
return call("ufsCreate", TIMEOUT_MS, () -> new JSONArray().put(new JSONObject() return call("ufsCreate", TIMEOUT_MS, () -> new JSONArray().put(new JSONObject()
.put("name", filename) .put("name", filename)
.put("size", filesize) .put("size", filesize)
.put("type", mimeType) .put("type", mimeType)
.put("store", "rocketchat_uploads") .put("store", store)
.put("rid", roomId) .put("rid", roomId)
)).onSuccessTask(CONVERT_TO_JSON_OBJECT); )).onSuccessTask(CONVERT_TO_JSON_OBJECT);
} }
public Task<JSONObject> ufsComplete(String fileId, String token) { public Task<JSONObject> ufsComplete(String fileId, String token, String store) {
return call("ufsComplete", TIMEOUT_MS, () -> new JSONArray() return call("ufsComplete", TIMEOUT_MS, () -> new JSONArray()
.put(fileId) .put(fileId)
.put("rocketchat_uploads") .put(store)
.put(token) .put(token)
).onSuccessTask(CONVERT_TO_JSON_OBJECT); ).onSuccessTask(CONVERT_TO_JSON_OBJECT);
} }
......
...@@ -9,6 +9,7 @@ import io.realm.annotations.PrimaryKey; ...@@ -9,6 +9,7 @@ import io.realm.annotations.PrimaryKey;
public class FileUploading extends RealmObject { 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";
@PrimaryKey private String uplId; @PrimaryKey private String uplId;
private int syncstate; private int syncstate;
......
...@@ -19,7 +19,7 @@ import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber; ...@@ -19,7 +19,7 @@ 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.GetUsersOfRoomsProcedureObserver; import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
import chat.rocket.android.service.observer.FileUploadingToGridFsObserver; import chat.rocket.android.service.observer.FileUploadingWithUfsObserver;
import chat.rocket.android.service.observer.LoadMessageProcedureObserver; import chat.rocket.android.service.observer.LoadMessageProcedureObserver;
import chat.rocket.android.service.observer.MethodCallObserver; import chat.rocket.android.service.observer.MethodCallObserver;
import chat.rocket.android.service.observer.NewMessageObserver; import chat.rocket.android.service.observer.NewMessageObserver;
...@@ -53,7 +53,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -53,7 +53,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
ReactiveNotificationManager.class, ReactiveNotificationManager.class,
NotificationItemObserver.class, NotificationItemObserver.class,
FileUploadingToS3Observer.class, FileUploadingToS3Observer.class,
FileUploadingToGridFsObserver.class FileUploadingWithUfsObserver.class
}; };
private final Context appContext; private final Context appContext;
private final String serverConfigId; private final String serverConfigId;
......
...@@ -26,10 +26,10 @@ import org.json.JSONObject; ...@@ -26,10 +26,10 @@ import org.json.JSONObject;
/** /**
* execute file uploading and requesting sendMessage with attachment. * execute file uploading and requesting sendMessage with attachment.
*/ */
public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUploading> { public class FileUploadingWithUfsObserver extends AbstractModelObserver<FileUploading> {
private FileUploadingHelper methodCall; private FileUploadingHelper methodCall;
public FileUploadingToGridFsObserver(Context context, String hostname, public FileUploadingWithUfsObserver(Context context, String hostname,
RealmHelper realmHelper, DDPClientWraper ddpClient) { RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, hostname, realmHelper, ddpClient); super(context, hostname, realmHelper, ddpClient);
methodCall = new FileUploadingHelper(realmHelper, ddpClient); methodCall = new FileUploadingHelper(realmHelper, ddpClient);
...@@ -38,7 +38,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl ...@@ -38,7 +38,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl
// resume pending operations. // resume pending operations.
RealmResults<FileUploading> pendingUploadRequests = realm.where(FileUploading.class) RealmResults<FileUploading> pendingUploadRequests = realm.where(FileUploading.class)
.equalTo("syncstate", SyncState.SYNCING) .equalTo("syncstate", SyncState.SYNCING)
.beginGroup()
.equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS) .equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS)
.or()
.equalTo("storageType", FileUploading.STORAGE_TYPE_FILE_SYSTEM)
.endGroup()
.findAll(); .findAll();
for (FileUploading req : pendingUploadRequests) { for (FileUploading req : pendingUploadRequests) {
req.setSyncstate(SyncState.NOT_SYNCED); req.setSyncstate(SyncState.NOT_SYNCED);
...@@ -51,7 +55,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl ...@@ -51,7 +55,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl
.or() .or()
.equalTo("syncstate", SyncState.FAILED) .equalTo("syncstate", SyncState.FAILED)
.endGroup() .endGroup()
.beginGroup()
.equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS) .equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS)
.or()
.equalTo("storageType", FileUploading.STORAGE_TYPE_FILE_SYSTEM)
.endGroup()
.findAll().deleteAllFromRealm(); .findAll().deleteAllFromRealm();
return null; return null;
}).continueWith(new LogcatIfError()); }).continueWith(new LogcatIfError());
...@@ -60,7 +68,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl ...@@ -60,7 +68,11 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl
@Override public RealmResults<FileUploading> queryItems(Realm realm) { @Override public RealmResults<FileUploading> queryItems(Realm realm) {
return realm.where(FileUploading.class) return realm.where(FileUploading.class)
.equalTo("syncstate", SyncState.NOT_SYNCED) .equalTo("syncstate", SyncState.NOT_SYNCED)
.beginGroup()
.equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS) .equalTo("storageType", FileUploading.STORAGE_TYPE_GRID_FS)
.or()
.equalTo("storageType", FileUploading.STORAGE_TYPE_FILE_SYSTEM)
.endGroup()
.findAll(); .findAll();
} }
...@@ -93,13 +105,17 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl ...@@ -93,13 +105,17 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl
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 store = FileUploading.STORAGE_TYPE_GRID_FS.equals(fileUploading.getStorageType())
? "rocketchat_uploads"
: (FileUploading.STORAGE_TYPE_FILE_SYSTEM.equals(fileUploading.getStorageType())
? "fileSystem" : null);
realmHelper.executeTransaction(realm -> realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject() realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject()
.put("uplId", uplId) .put("uplId", uplId)
.put("syncstate", SyncState.SYNCING) .put("syncstate", SyncState.SYNCING)
) )
).onSuccessTask(_task -> methodCall.ufsCreate(filename, filesize, mimeType, roomId) ).onSuccessTask(_task -> methodCall.ufsCreate(filename, filesize, mimeType, store, roomId)
).onSuccessTask(task -> { ).onSuccessTask(task -> {
final JSONObject info = task.getResult(); final JSONObject info = task.getResult();
final String fileId = info.getString("fileId"); final String fileId = info.getString("fileId");
...@@ -136,7 +152,7 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl ...@@ -136,7 +152,7 @@ public class FileUploadingToGridFsObserver extends AbstractModelObserver<FileUpl
} }
} }
return methodCall.ufsComplete(fileId, token); return methodCall.ufsComplete(fileId, token, store);
}).onSuccessTask(task -> methodCall.sendFileMessage(roomId, null, task.getResult()) }).onSuccessTask(task -> methodCall.sendFileMessage(roomId, null, task.getResult())
).onSuccessTask(task -> realmHelper.executeTransaction(realm -> ).onSuccessTask(task -> realmHelper.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject() realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject()
......
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