mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Do not use erroneous thumb url util function (#2901)
* Do not use erroneous thumb url util function * Remove unused code * Fix image loading for peer review * Clear disposable
This commit is contained in:
		
							parent
							
								
									70b19754fb
								
							
						
					
					
						commit
						a003e9706f
					
				
					 8 changed files with 28 additions and 88 deletions
				
			
		|  | @ -173,9 +173,6 @@ public class Media implements Parcelable { | |||
|      */ | ||||
|     @Nullable | ||||
|     public String getImageUrl() { | ||||
|         if (imageUrl == null && this.getFilename() != null) { | ||||
|             imageUrl = Utils.makeThumbBaseUrl(this.getFilename()); | ||||
|         } | ||||
|         return imageUrl; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -9,11 +9,8 @@ import android.net.Uri; | |||
| import android.view.View; | ||||
| import android.widget.Toast; | ||||
| 
 | ||||
| import org.apache.commons.codec.binary.Hex; | ||||
| import org.apache.commons.codec.digest.DigestUtils; | ||||
| import org.wikipedia.dataclient.WikiSite; | ||||
| import org.wikipedia.page.PageTitle; | ||||
| import org.wikipedia.util.UriUtil; | ||||
| 
 | ||||
| import java.util.Locale; | ||||
| import java.util.regex.Pattern; | ||||
|  | @ -34,18 +31,6 @@ public class Utils { | |||
|         return new PageTitle(title, new WikiSite(BuildConfig.COMMONS_URL)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Creates an URL for thumbnail | ||||
|      * | ||||
|      * @param filename Thumbnail file name | ||||
|      * @return URL of thumbnail | ||||
|      */ | ||||
|     public static String makeThumbBaseUrl(@NonNull String filename) { | ||||
|         String name = getPageTitle(filename).getPrefixedText(); | ||||
|         String sha = new String(Hex.encodeHex(DigestUtils.md5(name))); | ||||
|         return String.format("%s/%s/%s/%s", BuildConfig.IMAGE_URL_BASE, sha.substring(0, 1), sha.substring(0, 2), UriUtil.encodeURL(name)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Generates licence name with given ID | ||||
|      * @param license License ID | ||||
|  |  | |||
|  | @ -118,9 +118,8 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter { | |||
|                     Timber.d("Skipping %s", filename); | ||||
|                     continue; | ||||
|                 } | ||||
|                 String thumbUrl = Utils.makeThumbBaseUrl(filename); | ||||
|                 Date dateUpdated = image.getDateUpdated(); | ||||
|                 Contribution contrib = new Contribution(null, thumbUrl, filename, | ||||
|                 Contribution contrib = new Contribution(null, null, filename, | ||||
|                         "", -1, dateUpdated, dateUpdated, user, | ||||
|                         "", ""); | ||||
|                 contrib.setState(STATE_COMPLETED); | ||||
|  |  | |||
|  | @ -18,27 +18,20 @@ import java.util.ArrayList; | |||
| 
 | ||||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import androidx.appcompat.app.AlertDialog; | ||||
| import androidx.appcompat.widget.Toolbar; | ||||
| import androidx.drawerlayout.widget.DrawerLayout; | ||||
| import butterknife.BindView; | ||||
| import butterknife.ButterKnife; | ||||
| import butterknife.OnClick; | ||||
| import fr.free.nrw.commons.Media; | ||||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.Utils; | ||||
| import fr.free.nrw.commons.achievements.AchievementsActivity; | ||||
| import fr.free.nrw.commons.auth.AuthenticatedActivity; | ||||
| import fr.free.nrw.commons.delete.DeleteHelper; | ||||
| import fr.free.nrw.commons.mwapi.MediaWikiApi; | ||||
| import fr.free.nrw.commons.utils.DialogUtil; | ||||
| import fr.free.nrw.commons.utils.MediaDataExtractorUtil; | ||||
| import fr.free.nrw.commons.utils.ViewUtil; | ||||
| import io.reactivex.android.schedulers.AndroidSchedulers; | ||||
| import io.reactivex.disposables.CompositeDisposable; | ||||
| import io.reactivex.disposables.Disposable; | ||||
| import io.reactivex.schedulers.Schedulers; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| public class ReviewActivity extends AuthenticatedActivity { | ||||
| 
 | ||||
|  | @ -125,7 +118,6 @@ public class ReviewActivity extends AuthenticatedActivity { | |||
|         progressBar.setVisibility(View.VISIBLE); | ||||
|         reviewPager.setCurrentItem(0); | ||||
|         compositeDisposable.add(reviewHelper.getRandomMedia() | ||||
|                 .map(Media::getFilename) | ||||
|                 .subscribeOn(Schedulers.io()) | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|                 .subscribe(this::updateImage)); | ||||
|  | @ -133,37 +125,27 @@ public class ReviewActivity extends AuthenticatedActivity { | |||
|     } | ||||
| 
 | ||||
|     @SuppressLint("CheckResult") | ||||
|     private void updateImage(String fileName) { | ||||
|     private void updateImage(Media media) { | ||||
|         String fileName = media.getFilename(); | ||||
|         if (fileName.length() == 0) { | ||||
|             ViewUtil.showShortSnackbar(drawerLayout, R.string.error_review); | ||||
|             return; | ||||
|         } | ||||
|         simpleDraweeView.setImageURI(Utils.makeThumbBaseUrl(fileName)); | ||||
| 
 | ||||
|         simpleDraweeView.setImageURI(media.getImageUrl()); | ||||
| 
 | ||||
|         reviewController.onImageRefreshed(fileName); //file name is updated | ||||
|         compositeDisposable.add(reviewHelper.getFirstRevisionOfFile("File:" + fileName) | ||||
|         compositeDisposable.add(reviewHelper.getFirstRevisionOfFile(fileName) | ||||
|                 .subscribeOn(Schedulers.io()) | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|                 .subscribe(revision -> { | ||||
|                     reviewController.firstRevision = revision; | ||||
|                     reviewPagerAdapter.updateFileInformation(fileName); | ||||
|                     ((TextView) imageCaption).setText(fileName + " is uploaded by: " + revision.getUser()); | ||||
|                     imageCaption.setText(fileName + " is uploaded by: " + revision.getUser()); | ||||
|                     progressBar.setVisibility(View.GONE); | ||||
|                 })); | ||||
|         reviewPager.setCurrentItem(0); | ||||
| 
 | ||||
|         Disposable disposable = mwApi.fetchMediaByFilename("File:" + fileName) | ||||
|                 .subscribeOn(Schedulers.io()) | ||||
|                 .observeOn(AndroidSchedulers.mainThread()) | ||||
|                 .subscribe(mediaResult -> { | ||||
|                     ArrayList<String> categories = MediaDataExtractorUtil.extractCategories(mediaResult.getWikiSource()); | ||||
|                     updateCategories(categories); | ||||
|                 }, this::categoryFetchError); | ||||
|         compositeDisposable.add(disposable); | ||||
|     } | ||||
| 
 | ||||
|     private void categoryFetchError(Throwable throwable) { | ||||
|         Timber.e(throwable, "Error fetching categories"); | ||||
|         ViewUtil.showShortSnackbar(drawerLayout, R.string.error_review_categories); | ||||
|         updateCategories(media.getCategories()); | ||||
|     } | ||||
| 
 | ||||
|     private void updateCategories(ArrayList<String> categories) { | ||||
|  | @ -180,6 +162,12 @@ public class ReviewActivity extends AuthenticatedActivity { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onDestroy() { | ||||
|         super.onDestroy(); | ||||
|         compositeDisposable.clear(); | ||||
|     } | ||||
| 
 | ||||
|     public void showSkipImageInfo(){ | ||||
|         DialogUtil.showAlertDialog(ReviewActivity.this, | ||||
|                 getString(R.string.skip_image), | ||||
|  |  | |||
|  | @ -31,8 +31,13 @@ public class ReviewHelper { | |||
|         this.mediaWikiApi = mediaWikiApi; | ||||
|     } | ||||
| 
 | ||||
|     Single<Media> getRandomMedia() { | ||||
|         return getRandomFileChange() | ||||
|                 .flatMap(fileName -> okHttpJsonApiClient.getMedia(fileName, false)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Gets a random media file for review. | ||||
|      * Gets a random file change for review. | ||||
|      * - Picks the most recent changes in the last 30 day window | ||||
|      * - Picks a random file from those changes | ||||
|      * - Checks if the file is nominated for deletion | ||||
|  | @ -40,14 +45,14 @@ public class ReviewHelper { | |||
|      * | ||||
|      * @return | ||||
|      */ | ||||
|     Single<Media> getRandomMedia() { | ||||
|     private Single<String> getRandomFileChange() { | ||||
|         return okHttpJsonApiClient.getRecentFileChanges() | ||||
|                 .map(this::findImageInRecentChanges) | ||||
|                 .flatMap(title -> mediaWikiApi.pageExists("Commons:Deletion_requests/" + title) | ||||
|                         .map(pageExists -> new Pair<>(title, pageExists))) | ||||
|                 .map((Pair<String, Boolean> pair) -> { | ||||
|                     if (!pair.second) { | ||||
|                         return new Media(pair.first.replace("File:", "")); | ||||
|                         return pair.first; | ||||
|                     } | ||||
|                     throw new Exception("Already nominated for deletion"); | ||||
|                 }).retry(MAX_RANDOM_TRIES); | ||||
|  |  | |||
|  | @ -8,26 +8,6 @@ import java.util.regex.Matcher; | |||
| import java.util.regex.Pattern; | ||||
| 
 | ||||
| public class MediaDataExtractorUtil { | ||||
| 
 | ||||
|     /** | ||||
|      * We could fetch all category links from API, but we actually only want the ones | ||||
|      * directly in the page source so they're editable. In the future this may change. | ||||
|      * | ||||
|      * @param source wikitext source code | ||||
|      */ | ||||
|     public static ArrayList<String> extractCategories(String source) { | ||||
|         ArrayList<String> categories = new ArrayList<>(); | ||||
|         Pattern regex = Pattern.compile("\\[\\[\\s*Category\\s*:([^]]*)\\s*\\]\\]", Pattern.CASE_INSENSITIVE); | ||||
|         Matcher matcher = regex.matcher(source); | ||||
|         while (matcher.find()) { | ||||
|             String cat = matcher.group(1).trim(); | ||||
|             categories.add(cat); | ||||
|         } | ||||
| 
 | ||||
|         return categories; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * Extracts a list of categories from | separated category string | ||||
|      * | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vivek Maskara
						Vivek Maskara