-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GSoC'24] Enhancement of Media Drop Feature in NoteEditor #16749
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,17 @@ | |
*/ | ||
package com.ichi2.anki | ||
|
||
import android.content.ClipDescription | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.net.Uri | ||
import androidx.annotation.CheckResult | ||
import com.ichi2.anki.multimediacard.fields.ImageField | ||
import com.ichi2.anki.multimediacard.fields.MediaClipField | ||
import com.ichi2.compat.CompatHelper | ||
import com.ichi2.libanki.exception.EmptyMediaException | ||
import com.ichi2.utils.ClipboardUtil | ||
import com.ichi2.utils.ContentResolverUtil.getFileName | ||
import com.ichi2.utils.FileUtil.getFileNameAndExtension | ||
import timber.log.Timber | ||
|
@@ -47,7 +50,7 @@ class MediaRegistration(private val context: Context) { | |
* @return HTML referring to the loaded image | ||
*/ | ||
@Throws(IOException::class) | ||
fun loadMediaIntoCollection(uri: Uri): String? { | ||
fun loadMediaIntoCollection(uri: Uri, description: ClipDescription): String? { | ||
val fileName: String | ||
val filename = getFileName(context.contentResolver, uri) | ||
val fd = openInputStreamWithURI(uri) | ||
|
@@ -59,9 +62,12 @@ class MediaRegistration(private val context: Context) { | |
} | ||
var clipCopy: File | ||
var bytesWritten: Long | ||
val isImage = ClipboardUtil.hasImage(description) | ||
val isVideo = ClipboardUtil.hasVideo(description) | ||
|
||
openInputStreamWithURI(uri).use { copyFd -> | ||
// no conversion to jpg in cases of gif and jpg and if png image with alpha channel | ||
if (shouldConvertToJPG(fileNameAndExtension.value, copyFd)) { | ||
if (shouldConvertToJPG(fileNameAndExtension.value, copyFd, isImage)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have considered to send the |
||
clipCopy = File.createTempFile(fileName, ".jpg") | ||
bytesWritten = CompatHelper.compat.copyFile(fd, clipCopy.absolutePath) | ||
// return null if jpg conversion false. | ||
|
@@ -81,11 +87,22 @@ class MediaRegistration(private val context: Context) { | |
Timber.d("File was %d bytes", bytesWritten) | ||
if (bytesWritten > MEDIA_MAX_SIZE) { | ||
Timber.w("File was too large: %d bytes", bytesWritten) | ||
showThemedToast(context, context.getString(R.string.note_editor_paste_too_large), false) | ||
val message = if (isImage) { | ||
context.getString(R.string.note_editor_image_too_large) | ||
} else if (isVideo) { | ||
context.getString(R.string.note_editor_video_too_large) | ||
} else { | ||
context.getString(R.string.note_editor_audio_too_large) | ||
} | ||
showThemedToast(context, message, false) | ||
File(tempFilePath).delete() | ||
return null | ||
} | ||
val field = ImageField() | ||
val field = if (isImage) { | ||
ImageField() | ||
} else { | ||
MediaClipField() | ||
} | ||
david-allison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
field.hasTemporaryMedia = true | ||
field.mediaPath = tempFilePath | ||
return field.formattedValue | ||
|
@@ -112,7 +129,13 @@ class MediaRegistration(private val context: Context) { | |
return true // successful conversion to jpg. | ||
} | ||
|
||
private fun shouldConvertToJPG(fileNameExtension: String, fileStream: InputStream): Boolean { | ||
private fun shouldConvertToJPG(fileNameExtension: String, fileStream: InputStream, isImage: Boolean): Boolean { | ||
if (!isImage) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you are touching this file, would you mind:
|
||
return false | ||
} | ||
if (".svg" == fileNameExtension) { | ||
return false | ||
} | ||
if (".jpg" == fileNameExtension) { | ||
return false // we are already a jpg, no conversion | ||
} | ||
|
@@ -129,11 +152,11 @@ class MediaRegistration(private val context: Context) { | |
return fileNameAndExtension.key.length <= 3 | ||
} | ||
|
||
fun onPaste(uri: Uri): String? { | ||
fun onPaste(uri: Uri, description: ClipDescription): String? { | ||
return try { | ||
// check if cache already holds registered file or not | ||
if (!pastedMediaCache.containsKey(uri.toString())) { | ||
pastedMediaCache[uri.toString()] = loadMediaIntoCollection(uri) | ||
pastedMediaCache[uri.toString()] = loadMediaIntoCollection(uri, description) | ||
} | ||
pastedMediaCache[uri.toString()] | ||
} catch (ex: NullPointerException) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the commit message of the second commit, I'd suggest using
>
on the start of the lines, so that markdown displays it as a quoteThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need a space after > for it to work in markdown