Skip to content

Commit

Permalink
Merge branch 'maintenance' into origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Apr 17, 2020
2 parents 3240665 + 6088ed7 commit 95ec7fc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ allprojects {

ext {
projectGroupId = 'org.mariotaku.twidere'
projectVersionCode = 505
projectVersionName = '4.0.6'
projectVersionCode = 506
projectVersionName = '4.0.7'

globalCompileSdkVersion = 29
globalBuildToolsVersion = '29.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ MediaUploadResponse initUploadMedia(@Param("media_type") String mediaType,
@Params(@KeyValue(key = "command", value = "APPEND"))
ResponseCode appendUploadMedia(@Param("media_id") String mediaId,
@Param("segment_index") int segmentIndex,
@Param("media") Body media) throws MicroBlogException;
@Param("media_data") String media) throws MicroBlogException;

@POST("/media/upload.json")
@Params(@KeyValue(key = "command", value = "FINALIZE"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.core.app.NotificationCompat
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.core.app.NotificationCompat
import nl.komponents.kovenant.task
import nl.komponents.kovenant.ui.successUi
import org.mariotaku.abstask.library.AbstractTask
Expand All @@ -43,9 +44,7 @@ import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.microblog.library.twitter.TwitterUpload
import org.mariotaku.microblog.library.twitter.model.MediaUploadResponse
import org.mariotaku.microblog.library.twitter.model.MediaUploadResponse.ProcessingInfo
import org.mariotaku.restfu.http.ContentType
import org.mariotaku.restfu.http.mime.Body
import org.mariotaku.restfu.http.mime.SimpleBody
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
Expand All @@ -66,7 +65,9 @@ import org.mariotaku.twidere.task.CreateFavoriteTask
import org.mariotaku.twidere.task.RetweetStatusTask
import org.mariotaku.twidere.task.twitter.UpdateStatusTask
import org.mariotaku.twidere.task.twitter.message.SendMessageTask
import org.mariotaku.twidere.util.Utils
import org.mariotaku.twidere.util.deleteDrafts
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -323,20 +324,26 @@ class LengthyOperationsService : BaseIntentService("lengthy_operations") {
notificationManager.cancel(NOTIFICATION_ID_UPDATE_STATUS)
}


@Throws(IOException::class, MicroBlogException::class)
private fun uploadMedia(upload: TwitterUpload, body: Body): MediaUploadResponse {
val mediaType = body.contentType().contentType
val length = body.length()
val stream = body.stream()
var response = upload.initUploadMedia(mediaType, length, null, null)
val segments = if (length == 0L) 0 else (length / BULK_SIZE + 1).toInt()
for (segmentIndex in 0 until segments) {
val currentBulkSize = Math.min(BULK_SIZE, length - segmentIndex * BULK_SIZE).toInt()
val bulk = SimpleBody(ContentType.OCTET_STREAM, null, currentBulkSize.toLong(),
stream)
upload.appendUploadMedia(response.id, segmentIndex, bulk)
}
run {
var streamReadLength = 0
var segmentIndex = 0
while (streamReadLength < length) {
val currentBulkSize = Math.min(BULK_SIZE, length - streamReadLength).toInt()
val output = ByteArrayOutputStream()
Utils.copyStream(stream, output, currentBulkSize)
val data = Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
upload.appendUploadMedia(response.id, segmentIndex, data)
output.close()
segmentIndex++
streamReadLength += currentBulkSize
}
}
response = upload.finalizeUploadMedia(response.id)
run {
var info: ProcessingInfo? = response.processingInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import android.graphics.Point
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import android.util.Base64
import android.webkit.MimeTypeMap
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.exifinterface.media.ExifInterface
import android.text.TextUtils
import android.webkit.MimeTypeMap
import net.ypresto.androidtranscoder.MediaTranscoder
import net.ypresto.androidtranscoder.format.MediaFormatStrategyPresets
import org.mariotaku.ktextension.*
Expand All @@ -31,7 +32,6 @@ import org.mariotaku.microblog.library.twitter.model.StatusUpdate
import org.mariotaku.restfu.http.ContentType
import org.mariotaku.restfu.http.mime.Body
import org.mariotaku.restfu.http.mime.FileBody
import org.mariotaku.restfu.http.mime.SimpleBody
import org.mariotaku.sqliteqb.library.Expression
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
Expand All @@ -54,10 +54,7 @@ import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.io.ContentLengthInputStream
import org.mariotaku.twidere.util.premium.ExtraFeaturesService
import org.mariotaku.twidere.util.text.StatusTextValidator
import java.io.Closeable
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.io.*
import java.util.*
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -834,20 +831,26 @@ class UpdateStatusTask(
}
}


@Throws(IOException::class, MicroBlogException::class)
private fun uploadMediaChucked(upload: TwitterUpload, body: Body,
mediaCategory: String? = null, ownerIds: Array<String>?): MediaUploadResponse {
val mediaType = body.contentType().contentType
val length = body.length()
val stream = body.stream()
var response = upload.initUploadMedia(mediaType, length, mediaCategory, ownerIds)
val segments = if (length == 0L) 0 else (length / BULK_SIZE + 1).toInt()
for (segmentIndex in 0 until segments) {
val currentBulkSize = Math.min(BULK_SIZE.toLong(), length - segmentIndex * BULK_SIZE).toInt()
val bulk = SimpleBody(ContentType.OCTET_STREAM, null, currentBulkSize.toLong(),
stream)
upload.appendUploadMedia(response.id, segmentIndex, bulk)
run {
var streamReadLength = 0
var segmentIndex = 0
while (streamReadLength < length) {
val currentBulkSize = Math.min(BULK_SIZE.toLong(), length - streamReadLength).toInt()
val output = ByteArrayOutputStream()
Utils.copyStream(stream, output, currentBulkSize)
val data = Base64.encodeToString(output.toByteArray(), Base64.DEFAULT);
upload.appendUploadMedia(response.id, segmentIndex, data)
output.close()
segmentIndex++
streamReadLength += currentBulkSize
}
}
response = upload.finalizeUploadMedia(response.id)
var info: MediaUploadResponse.ProcessingInfo? = response.processingInfo
Expand Down
14 changes: 14 additions & 0 deletions twidere/src/main/kotlin/org/mariotaku/twidere/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ import org.mariotaku.twidere.provider.TwidereDataStore.CachedUsers
import org.mariotaku.twidere.util.TwidereLinkify.PATTERN_TWITTER_PROFILE_IMAGES
import org.mariotaku.twidere.view.TabPagerIndicator
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.util.*
import java.util.regex.Pattern

Expand Down Expand Up @@ -607,4 +609,16 @@ object Utils {

}

fun copyStream(input: InputStream, output: OutputStream, length: Int) {
val buffer = ByteArray(1024)
var bytesRead: Int = 0
do {
val read = input.read(buffer)
if (read == -1) {
break
}
output.write(buffer, 0, read)
bytesRead += read
} while (bytesRead <= length)
}
}

0 comments on commit 95ec7fc

Please sign in to comment.