mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +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();
|
||||
|
||||
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.setSource(source);
|
||||
}
|
||||
|
|
@ -70,12 +76,6 @@ public class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
|||
}
|
||||
|
||||
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) {
|
||||
Cursor cursor = context.getContentResolver().query(contribution.getLocalUri(),
|
||||
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package org.wikimedia.commons;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import android.graphics.*;
|
||||
import android.os.Bundle;
|
||||
|
|
@ -186,6 +189,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
this.startForeground(NOTIFICATION_UPLOAD_IN_PROGRESS, curProgressNotification.build());
|
||||
|
||||
try {
|
||||
String filename = findUniqueFilename(contribution.getFilename());
|
||||
if(!api.validateLogin()) {
|
||||
// Need to revalidate!
|
||||
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()),
|
||||
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()));
|
||||
|
|
@ -271,4 +275,49 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
contribution.setState(Contribution.STATE_FAILED);
|
||||
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