Only allow video upload when supported by instance

This commit is contained in:
Matthieu 2022-02-14 13:56:40 +01:00
parent 81d73b7a38
commit 63362a031b
8 changed files with 48 additions and 24 deletions

View File

@ -164,16 +164,17 @@ class PostCreationActivity : BaseActivity() {
}
for (i in 0 until count) {
clipData.getItemAt(i).uri.let {
val size = it.getSize()
val size = it.getSizeAndValidate()
photoData.add(PhotoData(imageUri = it, size = size))
}
}
}
/**
* Returns the size of the file of the Uri, and opens a dialog in case it is too big.
* Returns the size of the file of the Uri, and opens a dialog in case it is too big or in case
* the file is unsupported.
*/
private fun Uri.getSize(): Long {
private fun Uri.getSizeAndValidate(): Long {
val size: Long =
if (toString().startsWith("content")) {
contentResolver.query(this, null, null, null, null)
@ -191,16 +192,18 @@ class PostCreationActivity : BaseActivity() {
}
val sizeInkBytes = ceil(size.toDouble() / 1000).toLong()
val type = contentResolver.getType(this)
val isVideo = type?.startsWith("video/") == true
if(isVideo && !instance.videoEnabled){
AlertDialog.Builder(this@PostCreationActivity).apply {
setMessage(R.string.video_not_supported)
setNegativeButton(android.R.string.ok) { _, _ -> }
}.show()
}
if (sizeInkBytes > instance.maxPhotoSize || sizeInkBytes > instance.maxVideoSize) {
val maxSize = when {
instance.maxPhotoSize != instance.maxVideoSize -> {
val type = contentResolver.getType(this)
if (type?.startsWith("video/") == true) {
instance.maxVideoSize
} else instance.maxPhotoSize
}
else -> instance.maxPhotoSize
}
val maxSize = if (isVideo) instance.maxVideoSize else instance.maxPhotoSize
AlertDialog.Builder(this@PostCreationActivity).apply {
setMessage(getString(R.string.size_exceeds_instance_limit, photoData.size + 1, sizeInkBytes, maxSize))
setNegativeButton(android.R.string.ok) { _, _ -> }
@ -429,7 +432,7 @@ class PostCreationActivity : BaseActivity() {
val position: Int = result.data!!.getIntExtra(PhotoEditActivity.PICTURE_POSITION, 0)
photoData.getOrNull(position)?.apply {
imageUri = result.data!!.getStringExtra(PhotoEditActivity.PICTURE_URI)!!.toUri()
size = imageUri.getSize()
size = imageUri.getSizeAndValidate()
progress = null
uploadId = null
} ?: Toast.makeText(applicationContext, "Error while editing", Toast.LENGTH_SHORT).show()

View File

@ -41,6 +41,7 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.properties.Delegates
import org.pixeldroid.app.R
import org.pixeldroid.app.utils.BaseFragment
private const val ANIMATION_FAST_MILLIS = 50L
private const val ANIMATION_SLOW_MILLIS = 100L
@ -48,7 +49,7 @@ private const val ANIMATION_SLOW_MILLIS = 100L
/**
* Camera fragment
*/
class CameraFragment : Fragment() {
class CameraFragment : BaseFragment() {
private lateinit var container: ConstraintLayout
@ -314,11 +315,15 @@ class CameraFragment : Fragment() {
}
private fun setupUploadImage() {
val videoEnabled: Boolean = db.instanceDao().getInstance(db.userDao().getActiveUser()!!.instance_uri).videoEnabled
var mimeTypes: Array<String> = arrayOf("image/*")
if(videoEnabled) mimeTypes += "video/*"
// Listener for button used to view the most recent photo
binding.photoViewButton.setOnClickListener {
Intent(Intent.ACTION_GET_CONTENT).apply {
type = "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))
putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
action = Intent.ACTION_GET_CONTENT
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)

View File

@ -22,7 +22,7 @@ import org.pixeldroid.app.utils.api.objects.Notification
PublicFeedStatusDatabaseEntity::class,
Notification::class
],
version = 4
version = 5
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
@ -39,4 +39,9 @@ val MIGRATION_3_4 = object : Migration(3, 4) {
database.execSQL("DELETE FROM publicPosts")
database.execSQL("DELETE FROM notifications")
}
}
val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE instances ADD COLUMN videoEnabled INTEGER NOT NULL DEFAULT 1")
}
}

View File

@ -9,6 +9,7 @@ import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity.Companion.DEF
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_PHOTO_SIZE
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_TOOT_CHARS
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_VIDEO_SIZE
import org.pixeldroid.app.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_VIDEO_ENABLED
import org.pixeldroid.app.utils.normalizeDomain
import java.lang.IllegalArgumentException
@ -33,13 +34,14 @@ fun addUser(db: AppDatabase, account: Account, instance_uri: String, activeUser:
fun storeInstance(db: AppDatabase, nodeInfo: NodeInfo?, instance: Instance? = null) {
val dbInstance: InstanceDatabaseEntity = nodeInfo?.run {
InstanceDatabaseEntity(
uri = normalizeDomain(metadata?.config?.site?.url!!),
title = metadata.config.site.name!!,
maxStatusChars = metadata.config.uploader?.max_caption_length!!.toInt(),
maxPhotoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_PHOTO_SIZE,
//Pixelfed doesn't distinguish between max photo and video size
maxVideoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_VIDEO_SIZE,
albumLimit = metadata.config.uploader.album_limit?.toIntOrNull() ?: DEFAULT_ALBUM_LIMIT
uri = normalizeDomain(metadata?.config?.site?.url!!),
title = metadata.config.site.name!!,
maxStatusChars = metadata.config.uploader?.max_caption_length!!.toInt(),
maxPhotoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_PHOTO_SIZE,
// Pixelfed doesn't distinguish between max photo and video size
maxVideoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_VIDEO_SIZE,
albumLimit = metadata.config.uploader.album_limit?.toIntOrNull() ?: DEFAULT_ALBUM_LIMIT,
videoEnabled = metadata.config.features?.video ?: DEFAULT_VIDEO_ENABLED
)
} ?: instance?.run {
InstanceDatabaseEntity(

View File

@ -8,6 +8,9 @@ interface InstanceDao {
@Query("SELECT * FROM instances")
fun getAll(): List<InstanceDatabaseEntity>
@Query("SELECT * FROM instances WHERE uri=:instanceUri LIMIT 1")
fun getInstance(instanceUri: String): InstanceDatabaseEntity
/**
* Insert an instance, if it already exists return -1
*/

View File

@ -14,6 +14,8 @@ data class InstanceDatabaseEntity (
var maxVideoSize: Int = DEFAULT_MAX_VIDEO_SIZE,
// How many photos can go into an album. Default limit for Pixelfed and Mastodon is 4
var albumLimit: Int = DEFAULT_ALBUM_LIMIT,
// Is video functionality enabled on this instance?
var videoEnabled: Boolean = DEFAULT_VIDEO_ENABLED,
) {
companion object{
// Default max number of chars for Mastodon: used when their is no other value supplied by
@ -23,5 +25,6 @@ data class InstanceDatabaseEntity (
const val DEFAULT_MAX_PHOTO_SIZE = 8000
const val DEFAULT_MAX_VIDEO_SIZE = 40000
const val DEFAULT_ALBUM_LIMIT = 4
const val DEFAULT_VIDEO_ENABLED = true
}
}

View File

@ -6,6 +6,7 @@ import org.pixeldroid.app.utils.db.AppDatabase
import dagger.Module
import dagger.Provides
import org.pixeldroid.app.utils.db.MIGRATION_3_4
import org.pixeldroid.app.utils.db.MIGRATION_4_5
import javax.inject.Singleton
@Module
@ -17,6 +18,7 @@ class DatabaseModule(private val context: Context) {
return Room.databaseBuilder(
context,
AppDatabase::class.java, "pixeldroid"
).addMigrations(MIGRATION_3_4).allowMainThreadQueries().build()
).addMigrations(MIGRATION_3_4).addMigrations(MIGRATION_4_5)
.allowMainThreadQueries().build()
}
}

View File

@ -114,6 +114,7 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="no_media_description">Add a media description here…</string>
<string name="total_exceeds_album_limit">You chose more images than the maximum your server allows (%1$s). Images beyond the limit have been ignored.</string>
<string name="size_exceeds_instance_limit">Size of image number %1$d in the album exceeds the maximum size allowed by the instance (%2$d kB but the limit is %3$d kB). You might not be able to upload it.</string>
<string name="video_not_supported">"The server you are using doesn't support video uploads, you might not be able to upload videos included in this post"</string>
<string name="upload_error">Error code returned by server: %1$d</string>