|
| 1 | +package fr.free.nrw.commons.upload |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.ContentResolver |
| 5 | +import android.content.Context |
| 6 | +import android.database.Cursor |
| 7 | +import android.net.Uri |
| 8 | +import android.provider.MediaStore |
| 9 | +import android.text.TextUtils |
| 10 | +import fr.free.nrw.commons.R |
| 11 | +import fr.free.nrw.commons.auth.SessionManager |
| 12 | +import fr.free.nrw.commons.contributions.Contribution |
| 13 | +import fr.free.nrw.commons.kvstore.JsonKvStore |
| 14 | +import fr.free.nrw.commons.settings.Prefs |
| 15 | +import fr.free.nrw.commons.utils.ViewUtil.showLongToast |
| 16 | +import timber.log.Timber |
| 17 | +import java.io.BufferedInputStream |
| 18 | +import java.io.File |
| 19 | +import java.io.IOException |
| 20 | +import java.io.InputStream |
| 21 | +import java.util.Date |
| 22 | +import javax.inject.Inject |
| 23 | +import javax.inject.Singleton |
| 24 | + |
| 25 | +@Singleton |
| 26 | +class UploadController @Inject constructor( |
| 27 | + private val sessionManager: SessionManager, |
| 28 | + private val context: Context, |
| 29 | + private val store: JsonKvStore |
| 30 | +) { |
| 31 | + /** |
| 32 | + * Starts a new upload task. |
| 33 | + * |
| 34 | + * @param contribution the contribution object |
| 35 | + */ |
| 36 | + @SuppressLint("StaticFieldLeak") |
| 37 | + fun prepareMedia(contribution: Contribution) { |
| 38 | + //Set creator, desc, and license |
| 39 | + |
| 40 | + // If author name is enabled and set, use it |
| 41 | + |
| 42 | + val media = contribution.media |
| 43 | + if (store.getBoolean("useAuthorName", false)) { |
| 44 | + val authorName = store.getString("authorName", "") |
| 45 | + media.author = authorName |
| 46 | + } |
| 47 | + |
| 48 | + if (media.author.isNullOrEmpty()) { |
| 49 | + val currentAccount = sessionManager.currentAccount |
| 50 | + if (currentAccount == null) { |
| 51 | + Timber.d("Current account is null") |
| 52 | + showLongToast(context, context.getString(R.string.user_not_logged_in)) |
| 53 | + sessionManager.forceLogin(context) |
| 54 | + return |
| 55 | + } |
| 56 | + media.author = sessionManager.userName |
| 57 | + } |
| 58 | + |
| 59 | + if (media.fallbackDescription == null) { |
| 60 | + media.fallbackDescription = "" |
| 61 | + } |
| 62 | + |
| 63 | + val license = store.getString(Prefs.DEFAULT_LICENSE, Prefs.Licenses.CC_BY_SA_3) |
| 64 | + media.license = license |
| 65 | + |
| 66 | + buildUpload(contribution) |
| 67 | + } |
| 68 | + |
| 69 | + private fun buildUpload(contribution: Contribution) { |
| 70 | + val contentResolver = context.contentResolver |
| 71 | + |
| 72 | + contribution.dataLength = resolveDataLength(contentResolver, contribution) |
| 73 | + |
| 74 | + val mimeType = resolveMimeType(contentResolver, contribution) |
| 75 | + |
| 76 | + if (mimeType != null) { |
| 77 | + Timber.d("MimeType is: %s", mimeType) |
| 78 | + contribution.mimeType = mimeType |
| 79 | + if (mimeType.startsWith("image/") && contribution.dateCreated == null) { |
| 80 | + contribution.dateCreated = resolveDateTakenOrNow(contentResolver, contribution) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun resolveMimeType( |
| 86 | + contentResolver: ContentResolver, |
| 87 | + contribution: Contribution |
| 88 | + ): String? { |
| 89 | + val mimeType: String? = contribution.mimeType |
| 90 | + return if (mimeType.isNullOrEmpty() || mimeType.endsWith("*")) { |
| 91 | + contentResolver.getType(contribution.localUri!!) |
| 92 | + } else { |
| 93 | + mimeType |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private fun resolveDataLength( |
| 98 | + contentResolver: ContentResolver, |
| 99 | + contribution: Contribution |
| 100 | + ): Long { |
| 101 | + try { |
| 102 | + if (contribution.dataLength <= 0) { |
| 103 | + Timber.d( |
| 104 | + "UploadController/doInBackground, contribution.getLocalUri():%s", |
| 105 | + contribution.localUri |
| 106 | + ) |
| 107 | + |
| 108 | + contentResolver.openAssetFileDescriptor( |
| 109 | + Uri.fromFile(File(contribution.localUri!!.path!!)), "r" |
| 110 | + )?.use { |
| 111 | + return if (it.length != -1L) it.length |
| 112 | + else countBytes(contentResolver.openInputStream(contribution.localUri)) |
| 113 | + } |
| 114 | + } |
| 115 | + } catch (e: IOException) { |
| 116 | + Timber.e(e, "Exception occurred while uploading image") |
| 117 | + } catch (e: NullPointerException) { |
| 118 | + Timber.e(e, "Exception occurred while uploading image") |
| 119 | + } catch (e: SecurityException) { |
| 120 | + Timber.e(e, "Exception occurred while uploading image") |
| 121 | + } |
| 122 | + return contribution.dataLength |
| 123 | + } |
| 124 | + |
| 125 | + private fun resolveDateTakenOrNow( |
| 126 | + contentResolver: ContentResolver, |
| 127 | + contribution: Contribution |
| 128 | + ): Date { |
| 129 | + Timber.d("local uri %s", contribution.localUri) |
| 130 | + dateTakenCursor(contentResolver, contribution).use { cursor -> |
| 131 | + if (cursor != null && cursor.count != 0 && cursor.columnCount != 0) { |
| 132 | + cursor.moveToFirst() |
| 133 | + val dateCreated = Date(cursor.getLong(0)) |
| 134 | + if (dateCreated.after(Date(0))) { |
| 135 | + return dateCreated |
| 136 | + } |
| 137 | + } |
| 138 | + return Date() |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private fun dateTakenCursor( |
| 143 | + contentResolver: ContentResolver, |
| 144 | + contribution: Contribution |
| 145 | + ): Cursor? = contentResolver.query( |
| 146 | + contribution.localUri!!, |
| 147 | + arrayOf(MediaStore.Images.ImageColumns.DATE_TAKEN), null, null, null |
| 148 | + ) |
| 149 | + |
| 150 | + /** |
| 151 | + * Counts the number of bytes in `stream`. |
| 152 | + * |
| 153 | + * @param stream the stream |
| 154 | + * @return the number of bytes in `stream` |
| 155 | + * @throws IOException if an I/O error occurs |
| 156 | + */ |
| 157 | + @Throws(IOException::class) |
| 158 | + private fun countBytes(stream: InputStream?): Long { |
| 159 | + var count: Long = 0 |
| 160 | + val bis = BufferedInputStream(stream) |
| 161 | + while (bis.read() != -1) { |
| 162 | + count++ |
| 163 | + } |
| 164 | + return count |
| 165 | + } |
| 166 | +} |
0 commit comments