Skip to content

Bugfix/upload via share #1920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Binary file added app/libs/simplemagic-1.9.jar
Binary file not shown.
1 change: 0 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public static String fixExtension(String title, String extension) {

// 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;
Expand Down
37 changes: 31 additions & 6 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -130,7 +134,6 @@ public void onCreate() {
protected void handle(int what, Contribution contribution) {
switch (what) {
case ACTION_UPLOAD_FILE:
//FIXME: Google Photos bug
uploadContribution(contribution);
break;
default:
Expand Down Expand Up @@ -183,20 +186,34 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@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
Expand All @@ -222,9 +239,17 @@ private void uploadContribution(Contribution 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good documentation! 👍

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);
Expand Down