mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Merge pull request #17 from brion/namecheck
Name uniqueness check & extension fix
This commit is contained in:
commit
ef77d5ff49
2 changed files with 57 additions and 8 deletions
|
|
@ -29,7 +29,13 @@ public class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
||||||
|
|
||||||
app = (CommonsApplication)context.getApplicationContext();
|
app = (CommonsApplication)context.getApplicationContext();
|
||||||
|
|
||||||
contribution = new Contribution(mediaUri, null, rawTitle, description, -1, null, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
String title = rawTitle;
|
||||||
|
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
||||||
|
if(extension != null && !title.toLowerCase().endsWith(extension.toLowerCase())) {
|
||||||
|
title += "." + extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
contribution = new Contribution(mediaUri, null, title, description, -1, null, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
||||||
contribution.setTag("mimeType", mimeType);
|
contribution.setTag("mimeType", mimeType);
|
||||||
contribution.setSource(source);
|
contribution.setSource(source);
|
||||||
}
|
}
|
||||||
|
|
@ -70,12 +76,6 @@ public class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
||||||
}
|
}
|
||||||
|
|
||||||
String mimeType = (String)contribution.getTag("mimeType");
|
String mimeType = (String)contribution.getTag("mimeType");
|
||||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
|
||||||
|
|
||||||
if(extension != null && !title.toLowerCase().endsWith(extension.toLowerCase())) {
|
|
||||||
title += "." + extension;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mimeType.startsWith("image/") && contribution.getDateCreated() == null) {
|
if(mimeType.startsWith("image/") && contribution.getDateCreated() == null) {
|
||||||
Cursor cursor = context.getContentResolver().query(contribution.getLocalUri(),
|
Cursor cursor = context.getContentResolver().query(contribution.getLocalUri(),
|
||||||
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
package org.wikimedia.commons;
|
package org.wikimedia.commons;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
@ -186,6 +189,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
String filename = findUniqueFilename(contribution.getFilename());
|
||||||
if(!api.validateLogin()) {
|
if(!api.validateLogin()) {
|
||||||
// Need to revalidate!
|
// Need to revalidate!
|
||||||
if(app.revalidateAuthToken()) {
|
if(app.revalidateAuthToken()) {
|
||||||
|
|
@ -204,7 +208,7 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
String.format(getString(R.string.upload_progress_notification_title_finishing), contribution.getDisplayTitle()),
|
String.format(getString(R.string.upload_progress_notification_title_finishing), contribution.getDisplayTitle()),
|
||||||
contribution
|
contribution
|
||||||
);
|
);
|
||||||
result = api.upload(contribution.getFilename(), file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
|
result = api.upload(filename, file, contribution.getDataLength(), contribution.getPageContents(), contribution.getEditSummary(), notificationUpdater);
|
||||||
|
|
||||||
|
|
||||||
Log.d("Commons", "Response is" + Utils.getStringFromDOM(result.getDocument()));
|
Log.d("Commons", "Response is" + Utils.getStringFromDOM(result.getDocument()));
|
||||||
|
|
@ -271,4 +275,49 @@ public class UploadService extends HandlerService<Contribution> {
|
||||||
contribution.setState(Contribution.STATE_FAILED);
|
contribution.setState(Contribution.STATE_FAILED);
|
||||||
contribution.save();
|
contribution.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String findUniqueFilename(String fileName) {
|
||||||
|
return findUniqueFilename(fileName, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String findUniqueFilename(String fileName, int sequenceNumber) {
|
||||||
|
String sequenceFileName;
|
||||||
|
if (sequenceNumber == 1) {
|
||||||
|
sequenceFileName = fileName;
|
||||||
|
} else {
|
||||||
|
if (fileName.indexOf('.') == -1) {
|
||||||
|
// We really should have appended a file type suffix already.
|
||||||
|
// But... we might not.
|
||||||
|
sequenceFileName = fileName + " " + sequenceNumber;
|
||||||
|
} else {
|
||||||
|
Pattern regex = Pattern.compile("^(.*)(\\..+?)$");
|
||||||
|
Matcher regexMatcher = regex.matcher(fileName);
|
||||||
|
sequenceFileName = regexMatcher.replaceAll("$1 " + sequenceNumber + "$2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.d("Commons", "checking for uniqueness of name " + sequenceFileName);
|
||||||
|
|
||||||
|
if (fileExistsWithName(sequenceFileName)) {
|
||||||
|
return findUniqueFilename(fileName, sequenceNumber + 1);
|
||||||
|
} else {
|
||||||
|
return sequenceFileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean fileExistsWithName(String fileName) {
|
||||||
|
MWApi api = app.getApi();
|
||||||
|
ApiResult result;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = api.action("query")
|
||||||
|
.param("prop", "imageinfo")
|
||||||
|
.param("titles", "File:" + fileName)
|
||||||
|
.get();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<ApiResult> nodes = result.getNodes("/api/query/pages/page/imageinfo");
|
||||||
|
return nodes.size() > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue