Bugfix/upload via share (#1920)

* Bug Fix #1878
* Added a java library to fetch the MIME type from input stream
* Fetch MIME type using this and use the contribution tag only when this fails:

* formatting changes, removed unused commented out line
This commit is contained in:
Ashish Kumar 2018-10-11 11:24:43 +05:30 committed by Josephine Lim
parent 6a254741ef
commit 525d5da3d2
4 changed files with 32 additions and 8 deletions

View file

@ -73,13 +73,13 @@ dependencies {
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$LEAK_CANARY"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$LEAK_CANARY"
testImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$LEAK_CANARY"
implementation 'com.borjabravo:readmoretextview:2.1.0'
implementation 'com.dinuscxj:circleprogressbar:1.1.1'
implementation files('libs/simplemagic-1.9.jar')
}
android {

Binary file not shown.

View file

@ -131,7 +131,6 @@ public class Utils {
// If extension is still null, make it jpg. (Hotfix for https://github.com/commons-app/apps-android-commons/issues/228)
// If title has an extension in it, if won't be true
// FIXME: .png uploads fail when uploaded via Share
if (extension == null && title.lastIndexOf(".")<=0) {
extension = "jpg";
title += "." + extension;

View file

@ -14,9 +14,12 @@ import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
@ -69,6 +72,7 @@ public class UploadService extends HandlerService<Contribution> {
public static final int NOTIFICATION_UPLOAD_IN_PROGRESS = 1;
public static final int NOTIFICATION_UPLOAD_COMPLETE = 2;
public static final int NOTIFICATION_UPLOAD_FAILED = 3;
private ContentInfoUtil contentInfoUtil;
public UploadService() {
super("UploadService");
@ -130,7 +134,6 @@ public class UploadService extends HandlerService<Contribution> {
protected void handle(int what, Contribution contribution) {
switch (what) {
case ACTION_UPLOAD_FILE:
//FIXME: Google Photos bug
uploadContribution(contribution);
break;
default:
@ -183,20 +186,34 @@ public class UploadService extends HandlerService<Contribution> {
@SuppressLint("StringFormatInvalid")
private void uploadContribution(Contribution contribution) {
InputStream fileInputStream;
InputStream fileInputStream = null;
InputStream tempFileInputStream = null;
ContentInfo contentInfo = null;
String notificationTag = contribution.getLocalUri().toString();
try {
//FIXME: Google Photos bug
File file1 = new File(contribution.getLocalUri().getPath());
fileInputStream = new FileInputStream(file1);
//fileInputStream = this.getContentResolver().openInputStream(contribution.getLocalUri());
tempFileInputStream = new FileInputStream(file1);
if (contentInfoUtil == null) {
contentInfoUtil = new ContentInfoUtil();
}
contentInfo = contentInfoUtil.findMatch(tempFileInputStream);
} catch (FileNotFoundException e) {
Timber.d("File not found");
Toast fileNotFound = Toast.makeText(this, R.string.upload_failed, Toast.LENGTH_LONG);
fileNotFound.show();
return;
} catch (IOException e) {
Timber.d("exception while fetching MIME type: "+e);
} finally {
try {
if (null != tempFileInputStream) {
tempFileInputStream.close();
}
} catch (IOException e) {
Timber.d("File not found");
}
}
//As the fileInputStream is null there's no point in continuing the upload process
@ -222,9 +239,17 @@ public class UploadService extends HandlerService<Contribution> {
String filename = null;
try {
//try to fetch the MIME type from contentInfo first and then use the tag to do it
//Note : the tag has not proven trustworthy in the past
String mimeType;
if (contentInfo == null || contentInfo.getMimeType() == null) {
mimeType = (String) contribution.getTag("mimeType");
} else {
mimeType = contentInfo.getMimeType();
}
filename = Utils.fixExtension(
contribution.getFilename(),
MimeTypeMap.getSingleton().getExtensionFromMimeType((String)contribution.getTag("mimeType")));
MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType));
synchronized (unfinishedUploads) {
Timber.d("making sure of uniqueness of name: %s", filename);