下面列出了java.io.SyncFailedException#com.dropbox.client2.DropboxAPI 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initialize(String token) {
LogUtil.log(getClass().getSimpleName(), "initialize()");
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<>(session);
if ( token == null )
token = Settings.getDropboxSyncToken();
if ( token != null )
mDBApi.getSession().setOAuth2AccessToken(token);
if (!doesFolderExist(getBaseFilePath())) {
try {
createFileStructure();
} catch (DropboxException e) {
e.printStackTrace();
}
}
}
public void testRemoteFileMissing() {
DropboxAPI<?> dropboxapi = new DropboxAPIStub() {
public DropboxAPI.Entry metadata(String arg0, int arg1,
String arg2, boolean arg3, String arg4)
throws DropboxException {
throw notFoundException();
}
};
DropboxFileDownloader downloader = new DropboxFileDownloader(
dropboxapi, dropboxFiles1);
downloader.pullFiles();
assertEquals("Status should be SUCCESS", DropboxFileStatus.SUCCESS,
downloader.getStatus());
assertEquals("Should have 1 file", 1, downloader.getFiles().size());
assertEquals("Status should be NOT_FOUND", DropboxFileStatus.NOT_FOUND,
dbFile1.getStatus());
}
public void testRemoteFileDeleted() {
DropboxAPI<?> dropboxapi = new DropboxAPIStub() {
public DropboxAPI.Entry metadata(String arg0, int arg1,
String arg2, boolean arg3, String arg4)
throws DropboxException {
return create_metadata(remoterev1, true);
}
};
DropboxFileDownloader downloader = new DropboxFileDownloader(
dropboxapi, dropboxFiles1);
downloader.pullFiles();
assertEquals("Status should be SUCCESS", DropboxFileStatus.SUCCESS,
downloader.getStatus());
assertEquals("Should have 1 file", 1, downloader.getFiles().size());
assertEquals("Status should be NOT_FOUND", DropboxFileStatus.NOT_FOUND,
dbFile1.getStatus());
}
public void testBothFilesMissing() {
DropboxAPI<?> dropboxapi = new DropboxAPIStub() {
public DropboxAPI.Entry metadata(String arg0, int arg1,
String arg2, boolean arg3, String arg4)
throws DropboxException {
throw notFoundException();
}
};
DropboxFileDownloader downloader = new DropboxFileDownloader(
dropboxapi, dropboxFiles2);
downloader.pullFiles();
assertEquals("Status should be SUCCESS", DropboxFileStatus.SUCCESS,
downloader.getStatus());
assertEquals("Should have 2 files", 2, downloader.getFiles().size());
assertEquals("Status should be NOT_FOUND", DropboxFileStatus.NOT_FOUND,
dbFile1.getStatus());
assertEquals("Status should be NOT_FOUND", DropboxFileStatus.NOT_FOUND,
dbFile2.getStatus());
}
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
File file) {
// We set the context this way so we don't accidentally leak activities
mContext = context.getApplicationContext();
mFileLen = file.length();
mApi = api;
mPath = dropboxPath;
mFile = file;
mDialog = new ProgressDialog(context);
mDialog.setMax(100);
mDialog.setMessage("Uploading " + file.getName());
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setProgress(0);
mDialog.setButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// This will cancel the putFile operation
mRequest.abort();
}
});
mDialog.show();
}
public UploadFile(Context context, DropboxAPI<?> api, String dropboxPath,
File file) {
// We set the context this way so we don't accidentally leak activities
mContext = context.getApplicationContext();
mFileLen = file.length();
mApi = api;
mPath = dropboxPath;
mFile = file;
mDialog = new ProgressDialog(context);
mDialog.setMax(100);
mDialog.setMessage("Uploading " + file.getName());
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setProgress(0);
mDialog.setButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// This will cancel the putFile operation
mRequest.abort();
}
});
mDialog.show();
}
public UploadAudioFile(Context context, DropboxAPI dropboxApi, String dropbox_path, String filePath, String fileName) {
super();
this.dropboxApi = dropboxApi;
this.dropbox_path = dropbox_path;
this.context = context;
this.filePath = filePath;
this.fileName = fileName;
}
@Override
public List<RemoteDataInfo> getRemoteEntries() throws SyncFailedException {
StringBuilder sb = new StringBuilder();
sb.append(getBaseFilePath());
sb.append(ENTRIES);
List<RemoteDataInfo> dataInfoObjects = new ArrayList<>();
try {
List<DropboxAPI.Entry> dropboxEntries = getFileInfo(sb.toString()).contents;
for (DropboxAPI.Entry entry : dropboxEntries) {
if ( !entry.isDir ) {
RemoteDataInfo infoObject = new RemoteDataInfo();
infoObject.isDirectory = entry.isDir;
infoObject.isDeleted = entry.isDeleted;
infoObject.name = entry.fileName().toUpperCase();
infoObject.modifiedDate = RESTUtility.parseDate(entry.modified).getTime();
infoObject.revision = entry.rev;
dataInfoObjects.add(infoObject);
}
}
} catch (Exception e) {
if (!BuildConfig.DEBUG) Crashlytics.logException(e);
e.printStackTrace();
throw new SyncFailedException(e.getMessage());
}
return dataInfoObjects;
}
@Override
public List<RemoteDataInfo> getRemotePhotos() throws SyncFailedException {
StringBuilder sb = new StringBuilder();
sb.append(getBaseFilePath());
sb.append(PHOTOS);
List<RemoteDataInfo> dataInfoObjects = new ArrayList<>();
try {
List<DropboxAPI.Entry> dropboxEntries = getFileInfo(sb.toString()).contents;
for (DropboxAPI.Entry file : dropboxEntries) {
if ( !file.isDir ) {
RemoteDataInfo infoObject = new RemoteDataInfo();
infoObject.isDirectory = file.isDir;
infoObject.isDeleted = file.isDeleted;
infoObject.name = file.fileName().toLowerCase();
infoObject.modifiedDate = RESTUtility.parseDate(file.modified).getTime();
infoObject.revision = file.rev;
dataInfoObjects.add(infoObject);
}
}
} catch (Exception e) {
if (!BuildConfig.DEBUG) Crashlytics.logException(e);
e.printStackTrace();
throw new SyncFailedException(e.getMessage());
}
return dataInfoObjects;
}
private boolean doesFolderExist(String path) {
try {
DropboxAPI.Entry metadata = getFileInfo(path);
return metadata.isDir;
} catch (DropboxException e) {
e.printStackTrace();
return false;
}
}
/**
* Returns 1 if it does exist
* Returns 0 if it does not exist
* Returns -1 if there is any other error
*
* @param path
* @return
*/
private int doesFileExist(String path) {
try {
DropboxAPI.Entry metadata = getFileInfo(path);
return 1;
} catch (DropboxException e) {
e.printStackTrace();
if (e.toString().contains("404"))
return 0;
else
return -1;
}
}
@Override
public com.dropbox.client2.DropboxAPI.DropboxFileInfo getThumbnail(
String arg0, OutputStream arg1,
com.dropbox.client2.DropboxAPI.ThumbSize arg2,
com.dropbox.client2.DropboxAPI.ThumbFormat arg3, ProgressListener arg4)
throws DropboxException {
return null;
}
private DropboxAPI.Entry create_metadata(String path, String rev, boolean isDeleted) {
DropboxAPI.Entry metadata = new DropboxAPI.Entry();
metadata.path = path;
metadata.rev = rev;
metadata.isDeleted = isDeleted;
return metadata;
}
public UploadFile(Context context, DropboxAPI dropboxApi, String path) {
super();
this.dropboxApi = dropboxApi;
this.path = path;
this.context = context;
}
public DropBoxHelper(Context context) {
mContext = context;
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<>(session);
}
public DropboxAPI<AndroidAuthSession> getApi() {
return mApi;
}
@Override
public void save(Entry entry) {
LogUtil.log(getClass().getSimpleName(), "save()");
// update sync status
SyncInfo info = new SyncInfo();
info.setTitle(entry.uuid);
info.setSyncService(SyncService.Dropbox.ordinal());
info.setModifiedDate(Calendar.getInstance(Locale.getDefault()).getTimeInMillis());
info.setSyncStatus(SyncStatus.UPLOAD);
SyncInfoDao.saveData(info);
// save to user's dropbox
String path = getPathForEntry(entry);
File tmpFile = new File(GlobalApplication.getAppContext().getFilesDir() + File.separator + entry.uuid);
DropboxAPI.Entry response = null;
try {
if (!tmpFile.exists())
tmpFile.createNewFile();
NSDictionary entryData = EntryHelper.toDictionary(entry);
PropertyListParser.saveAsXML(entryData, tmpFile);
// overwrite the entry if it already exists
if (doesContain(entry))
response = overwriteFile(path, tmpFile);
else
response = uploadFile(path, tmpFile);
info.setModifiedDate(RESTUtility.parseDate(response.modified).getTime());
info.setSyncStatus(SyncStatus.OK);
info.setRevision(response.rev);
SyncInfoDao.saveData(info);
} catch (Exception e) {
LogUtil.e(getClass().getSimpleName(), "Error Saving Entry: ", e);
} finally {
tmpFile.delete();
}
}
@Override
public void delete(Entry entry) {
LogUtil.log(getClass().getSimpleName(), "delete()");
// update sync status
SyncInfo info = new SyncInfo(entry.uuid);
info.setSyncService(SyncService.Dropbox.ordinal());
info.setModifiedDate(Calendar.getInstance(Locale.getDefault()).getTimeInMillis());
info.setSyncStatus(SyncStatus.DELETE);
SyncInfoDao.saveData(info);
// determine the path to the entry, if it exists
StringBuilder from = new StringBuilder();
StringBuilder to = new StringBuilder();
boolean dayOneEntry;
from.append(getBaseFilePath());
to.append(from.toString());
from.append(ENTRIES);
from.append(File.separator);
from.append(entry.uuid.toUpperCase());
to.append(DELETED_ENTRIES);
to.append(File.separator);
to.append(entry.uuid.toUpperCase());
dayOneEntry = !(doesFileExist(from.toString()) == 1);
if (dayOneEntry) {
from.append(".doentry");
to.append(".doentry");
}
// attempt to delete from Dropbox
try {
DropboxAPI.Entry response = moveFile(from.toString(), to.toString());
info.setModifiedDate(RESTUtility.parseDate(response.modified).getTime());
info.setSyncStatus(SyncStatus.OK);
SyncInfoDao.saveData(info);
} catch (DropboxException e) {
e.printStackTrace();
if (e.toString().contains("404")) {
info.setSyncStatus(SyncStatus.OK);
SyncInfoDao.saveData(info);
} else {
info.setSyncStatus(SyncStatus.DELETE);
SyncInfoDao.saveData(info);
}
}
}
@Override
public boolean delete(Photo photo) {
LogUtil.log(getClass().getSimpleName(), "delete(Photo)");
// update sync status
SyncInfo info = new SyncInfo(photo.name.toUpperCase());
info.setSyncService(SyncService.Dropbox.ordinal());
info.setModifiedDate(Calendar.getInstance(Locale.getDefault()).getTimeInMillis());
info.setSyncStatus(SyncStatus.DELETE);
SyncInfoDao.saveData(info);
String from = getPathForPhoto(photo.name);
StringBuilder to = new StringBuilder();
to.append(getBaseFilePath());
to.append(DELETED_PHOTOS);
to.append(File.separator);
to.append(photo.name);
// attempt to delete from Dropbox
try {
DropboxAPI.Entry response = moveFile(from, to.toString());
info.setModifiedDate(RESTUtility.parseDate(response.modified).getTime());
info.setSyncStatus(SyncStatus.OK);
SyncInfoDao.saveData(info);
return true;
} catch (DropboxException e) {
e.printStackTrace();
if (e.toString().contains("404")) {
// this may get thrown if the entry's photo has been deleted/removed/changed before
// and there exists a file in the /Narrate/photos/deleted/ with the same name as
// the one we are trying to save.
try {
mDBApi.delete(to.toString());
moveFile(from, to.toString());
} catch (DropboxException e1) {
e1.printStackTrace();
}
info.setSyncStatus(SyncStatus.OK);
SyncInfoDao.saveData(info);
return true;
} else {
info.setSyncStatus(SyncStatus.DELETE);
SyncInfoDao.saveData(info);
}
}
return false;
}
private DropboxAPI.Entry getFileInfo(String path) throws DropboxException {
checkNotNull();
return mDBApi.metadata(path, 5000, null, true, null);
}
public DropboxAPI.Entry uploadFile(String dbPath, File localFile) throws FileNotFoundException, DropboxException {
checkNotNull();
FileInputStream inputStream = new FileInputStream(localFile);
return mDBApi.putFile(dbPath, inputStream,
localFile.length(), null, null);
}
public DropboxAPI.Entry overwriteFile(String dbPath, File localFile) throws DropboxException, FileNotFoundException {
checkNotNull();
FileInputStream inputStream = new FileInputStream(localFile);
return mDBApi.putFileOverwrite(dbPath, inputStream,
localFile.length(), null);
}
private DropboxAPI.Entry moveFile(String from, String to) throws DropboxException {
checkNotNull();
return mDBApi.move(from, to);
}
private DropboxAPI.Entry create_metadata(String rev) {
return create_metadata(rev, false);
}
private DropboxAPI.Entry create_metadata(String rev, boolean isDeleted) {
DropboxAPI.Entry metadata = new DropboxAPI.Entry();
metadata.rev = rev;
metadata.isDeleted = isDeleted;
return metadata;
}
@Override
public com.dropbox.client2.DropboxAPI.Account accountInfo()
throws DropboxException {
return null;
}
@Override
public com.dropbox.client2.DropboxAPI.Entry copy(String arg0, String arg1)
throws DropboxException {
return null;
}
@Override
public com.dropbox.client2.DropboxAPI.Entry createFolder(String arg0)
throws DropboxException {
return null;
}
@Override
public com.dropbox.client2.DropboxAPI.DropboxFileInfo getFile(String arg0,
String arg1, OutputStream arg2, ProgressListener arg3)
throws DropboxException {
return null;
}
@Override
public com.dropbox.client2.DropboxAPI.DropboxInputStream getFileStream(
String arg0, String arg1) throws DropboxException {
return null;
}