Pushing files to the server successfully

pull/14/head
Sam Bosley 13 years ago
parent 8822ffedf7
commit 5546f97a83

@ -18,6 +18,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody; import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -587,6 +588,41 @@ public final class ActFmSyncService {
} }
} }
/**
* Push a file attachment to the server
* @param remoteTaskId
* @param fileMetadata
*/
public void pushAttachment(long remoteTaskId, Metadata fileMetadata) {
if (!fileMetadata.containsNonNullValue(FileMetadata.FILE_PATH) || remoteTaskId <= 0)
return;
File f = new File(fileMetadata.getValue(FileMetadata.FILE_PATH));
if (!f.exists())
return;
ArrayList<Object> params = new ArrayList<Object>();
params.add("task_id"); params.add(remoteTaskId);
params.add("token"); params.add(token);
try {
MultipartEntity entity = new MultipartEntity();
FileBody body = new FileBody(f, fileMetadata.getValue(FileMetadata.FILE_TYPE));
entity.addPart("file", body);
JSONObject result = actFmInvoker.post("task_attachment_create", entity,
params.toArray(new Object[params.size()]));
fileMetadata.setValue(FileMetadata.REMOTE_ID, result.optLong("id"));
fileMetadata.setValue(FileMetadata.URL, result.optString("url"));
metadataService.save(fileMetadata);
} catch (ActFmServiceException e) {
handleException("push-attacgment-error", e);
} catch (IOException e) {
handleException("push-attacgment-error", e);
}
}
// --- data fetch methods // --- data fetch methods
/** /**

@ -14,6 +14,7 @@ import org.json.JSONObject;
import com.timsu.astrid.C2DMReceiver; import com.timsu.astrid.C2DMReceiver;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
@ -21,13 +22,15 @@ import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Join; import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.User; import com.todoroo.astrid.data.User;
import com.todoroo.astrid.files.FileMetadata;
import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.AstridDependencyInjector;
import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.service.TagDataService;
import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.sync.SyncResultCallback; import com.todoroo.astrid.sync.SyncResultCallback;
@ -50,6 +53,8 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
@Autowired TagDataService tagDataService; @Autowired TagDataService tagDataService;
@Autowired MetadataService metadataService;
private final PushQueuedArgs<Task> taskPusher = new PushQueuedArgs<Task>() { private final PushQueuedArgs<Task> taskPusher = new PushQueuedArgs<Task>() {
@Override @Override
public Task getRemoteModelInstance(TodorooCursor<Task> cursor) { public Task getRemoteModelInstance(TodorooCursor<Task> cursor) {
@ -80,6 +85,24 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
} }
}; };
private final PushQueuedArgs<Metadata> filesPusher = new PushQueuedArgs<Metadata>() {
@Override
public void pushRemoteModel(Metadata model) {
long taskId = model.getValue(Metadata.TASK);
Task localTask = taskService.fetchById(taskId, Task.REMOTE_ID);
long remoteTaskId = localTask.getValue(Task.REMOTE_ID);
if (remoteTaskId <= 0)
return;
actFmSyncService.pushAttachment(remoteTaskId, model);
};
public Metadata getRemoteModelInstance(TodorooCursor<Metadata> cursor) {
return new Metadata(cursor);
}
};
static { static {
AstridDependencyInjector.initialize(); AstridDependencyInjector.initialize();
} }
@ -228,12 +251,12 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
}); });
} }
private static interface PushQueuedArgs<T extends RemoteModel> { private static interface PushQueuedArgs<T extends AbstractModel> {
public T getRemoteModelInstance(TodorooCursor<T> cursor); public T getRemoteModelInstance(TodorooCursor<T> cursor);
public void pushRemoteModel(T model); public void pushRemoteModel(T model);
} }
private <T extends RemoteModel> void pushQueued(final SyncResultCallback callback, final AtomicInteger finisher, private <T extends AbstractModel> void pushQueued(final SyncResultCallback callback, final AtomicInteger finisher,
TodorooCursor<T> cursor, boolean awaitTermination, final PushQueuedArgs<T> pusher) { TodorooCursor<T> cursor, boolean awaitTermination, final PushQueuedArgs<T> pusher) {
try { try {
callback.incrementMax(cursor.getCount() * 20); callback.incrementMax(cursor.getCount() * 20);
@ -278,7 +301,13 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
Criterion.and(Task.REMOTE_ID.isNotNull(), Criterion.and(Task.REMOTE_ID.isNotNull(),
Task.MODIFICATION_DATE.gt(Task.LAST_SYNC))))); Task.MODIFICATION_DATE.gt(Task.LAST_SYNC)))));
pushQueued(callback, finisher, taskCursor, false, taskPusher); TodorooCursor<Metadata> filesCursor = metadataService.query(Query.select(Metadata.PROPERTIES)
.where(Criterion.and(
MetadataCriteria.withKey(FileMetadata.METADATA_KEY),
FileMetadata.REMOTE_ID.eq(0))));
pushQueued(callback, finisher, taskCursor, true, taskPusher);
pushQueued(callback, finisher, filesCursor, false, filesPusher);
} }
private void pushQueuedTags(final SyncResultCallback callback, private void pushQueuedTags(final SyncResultCallback callback,

@ -16,6 +16,8 @@ public class FileMetadata {
public static final String FILE_TYPE_AUDIO = "audio/m4a"; //$NON-NLS-1$ public static final String FILE_TYPE_AUDIO = "audio/m4a"; //$NON-NLS-1$
public static final String FILE_TYPE_IMG = "image/png"; //$NON-NLS-1$ public static final String FILE_TYPE_IMG = "image/png"; //$NON-NLS-1$
public static final String FILE_TYPE_OTHER = "application/octet-stream"; //$NON-NLS-1$
public static final StringProperty FILE_PATH = new StringProperty(Metadata.TABLE, public static final StringProperty FILE_PATH = new StringProperty(Metadata.TABLE,
Metadata.VALUE1.name); Metadata.VALUE1.name);

@ -1053,7 +1053,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
return; return;
} }
createNewFileAttachment(dst.getAbsolutePath(), ""); //$NON-NLS-1$ createNewFileAttachment(dst.getAbsolutePath(), FileMetadata.FILE_TYPE_OTHER);
} }
@SuppressWarnings("nls") @SuppressWarnings("nls")

Loading…
Cancel
Save