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

upload to FileSystem.

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