Record file attachment outstanding entries and report them using special ChangesHappened logic

pull/14/head
Sam Bosley 12 years ago
parent 3e953d34df
commit dd96750138

@ -10,6 +10,7 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -153,6 +154,32 @@ public class AndroidUtilities {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private static final int BUFFER_SIZE = 4096;
public static String encodeBase64File(File file) {
try {
FileInputStream in = new FileInputStream(file);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
byte[] bytes = baos.toByteArray();
return Base64.encodeToString(bytes, Base64.DEFAULT);
} finally {
in.close();
}
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
}
}
/**
* Start the given intent, handling security exceptions if they arise
*

@ -144,6 +144,7 @@ public final class TaskAttachment extends RemoteModel {
TaskAttachment attachment = new TaskAttachment();
attachment.setValue(TaskAttachment.TASK_UUID, taskUuid);
attachment.setValue(NAME, fileName);
attachment.setValue(USER_UUID, Task.USER_ID_SELF);
attachment.setValue(FILE_PATH, filePath);
attachment.setValue(CONTENT_TYPE, fileType);
attachment.setValue(DELETED_AT, 0L);

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@ -7,6 +8,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;
import android.util.Log;
import com.todoroo.andlib.data.Property;
@ -14,6 +16,7 @@ import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType;
@ -110,6 +113,15 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
} else if (NameMaps.MEMBER_REMOVED_COLUMN.equals(localColumn)) {
serverColumn = NameMaps.MEMBER_REMOVED_COLUMN;
changeJson.put("value", change.getValue(OutstandingEntry.VALUE_STRING_PROPERTY));
} else if (NameMaps.ATTACHMENT_ADDED_COLUMN.equals(localColumn)) {
serverColumn = NameMaps.ATTACHMENT_ADDED_COLUMN;
JSONObject fileJson = getFileJson(change.getValue(OutstandingEntry.VALUE_STRING_PROPERTY));
if (fileJson == null) {
PluginServices.getTaskAttachmentDao().delete(id);
continue;
} else {
changeJson.put("value", fileJson.toString());
}
} else {
Property<?> localProperty = NameMaps.localColumnNameToProperty(table, localColumn);
if (localProperty == null)
@ -155,6 +167,28 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
}
}
private JSONObject getFileJson(String value) {
try {
JSONObject obj = new JSONObject(value);
String path = obj.optString("path");
if (TextUtils.isEmpty(path))
return null;
File f = new File(path);
if (!f.exists())
return null;
String encodedFile = AndroidUtilities.encodeBase64File(f);
if (TextUtils.isEmpty(encodedFile))
return null;
obj.remove("path");
obj.put("data", encodedFile);
return obj;
} catch (JSONException e) {
return null;
}
}
private class PropertyToJSONVisitor implements PropertyVisitor<Object, OE> {
private String getAsString(OE data) {

@ -255,6 +255,8 @@ public class NameMaps {
private static final Map<String, Property<?>> TASK_ATTACHMENT_PROPERTIES_SERVER_TO_LOCAL;
private static final Set<String> TASK_ATTACHMENT_PROPERTIES_EXCLUDED;
public static final String ATTACHMENT_ADDED_COLUMN = "file";
private static void putTaskAttachmentPropertyToServerName(Property<?> property, String serverName, boolean writeable) {
putPropertyToServerName(property, serverName, TASK_ATTACHMENT_PROPERTIES_LOCAL_TO_SERVER, TASK_ATTACHMENT_COLUMN_NAMES_TO_PROPERTIES,
TASK_ATTACHMENT_COLUMNS_LOCAL_TO_SERVER, TASK_ATTACHMENT_PROPERTIES_EXCLUDED, writeable);

@ -46,6 +46,7 @@ import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.dao.TaskAttachmentDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.ui.PopupControlSet;
@ -256,6 +257,7 @@ public class FilesControlSet extends PopupControlSet {
useType = guessedType;
if (useType != guessedType) {
m.setValue(TaskAttachment.CONTENT_TYPE, useType);
m.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
taskAttachmentDao.saveExisting(m);
}
}

@ -5,14 +5,23 @@
*/
package com.todoroo.astrid.dao;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ContentValues;
import android.text.TextUtils;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.data.OutstandingEntry;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.TaskAttachment;
import com.todoroo.astrid.data.TaskAttachmentOutstanding;
/**
* Data Access layer for {@link TagData}-related operations.
@ -36,6 +45,34 @@ public class TaskAttachmentDao extends RemoteModelDao<TaskAttachment> {
return NameMaps.shouldRecordOutstandingColumnForTable(NameMaps.TABLE_ID_ATTACHMENTS, columnName);
}
@Override
protected int createOutstandingEntries(long modelId, ContentValues modelSetValues) {
// new attachment case -- only set by us when creating new attachments; when setting during sync outstanding entries suppressed
if (modelSetValues.containsKey(TaskAttachment.CONTENT_TYPE.name)) {
TaskAttachmentOutstanding m = new TaskAttachmentOutstanding();
String path = modelSetValues.getAsString(TaskAttachment.FILE_PATH.name);
if (TextUtils.isEmpty(path))
return -1;
try {
JSONObject newFileHash = new JSONObject();
newFileHash.put("name", modelSetValues.getAsString(TaskAttachment.NAME.name)); //$NON-NLS-1$
newFileHash.put("type", modelSetValues.getAsString(TaskAttachment.CONTENT_TYPE.name)); //$NON-NLS-1$
newFileHash.put("path", modelSetValues.getAsString(path)); //$NON-NLS-1$
m.setValue(OutstandingEntry.ENTITY_ID_PROPERTY, modelId);
m.setValue(OutstandingEntry.COLUMN_STRING_PROPERTY, NameMaps.ATTACHMENT_ADDED_COLUMN);
m.setValue(OutstandingEntry.VALUE_STRING_PROPERTY, newFileHash.toString());
m.setValue(OutstandingEntry.CREATED_AT_PROPERTY, DateUtilities.now());
} catch (JSONException e) {
return -1;
}
}
int result = super.createOutstandingEntries(modelId, modelSetValues);
if (result < 0) // Error
return result;
return 1 + result;
}
public boolean taskHasAttachments(String taskUuid) {
TodorooCursor<TaskAttachment> files = query(Query.select(TaskAttachment.TASK_UUID).where(
Criterion.and(TaskAttachment.TASK_UUID.eq(taskUuid),

Loading…
Cancel
Save