@@ -19,11 +19,13 @@ package com.ichi2.anki.libanki
1919
2020import androidx.annotation.WorkerThread
2121import anki.media.CheckMediaResponse
22+ import com.google.protobuf.ByteString
2223import com.google.protobuf.kotlin.toByteString
2324import com.ichi2.anki.libanki.Media.Companion.htmlMediaRegexps
2425import com.ichi2.anki.libanki.exception.EmptyMediaException
2526import com.ichi2.anki.libanki.utils.LibAnkiAlias
2627import com.ichi2.anki.libanki.utils.NotInLibAnki
28+ import net.ankiweb.rsdroid.RustCleanup
2729import timber.log.Timber
2830import java.io.File
2931
@@ -62,17 +64,40 @@ open class Media(
6264 */
6365
6466 /* *
65- * @throws EmptyMediaException if [oFile] is empty or null
67+ * Add [filename][File.name] to the media folder, renaming if not unique
68+ *
69+ * @return possibly-renamed filename
70+ *
71+ * @throws EmptyMediaException if [file] is empty
6672 * @throws OutOfMemoryError if the file could not be copied to a contiguous block of memory (or is >= 2GB)
6773 */
68- fun addFile (oFile : File ? ): String {
69- if (oFile == null || oFile.length() == 0L ) {
74+ @LibAnkiAlias(" add_file" )
75+ @RustCleanup(" use backend exception instead of EmptyMediaException" )
76+ fun addFile (file : File ? ): String {
77+ // fail if non-existing or empty
78+ if (file == null || file.length() == 0L ) {
7079 throw EmptyMediaException ()
7180 }
72- Timber .v(" dir now %s" , dir)
73- return col.backend.addMediaFile(oFile.name, oFile.readBytes().toByteString())
81+ return writeData(file.name, file.readBytes().toByteString())
7482 }
7583
84+ /* *
85+ * Write the file to the media folder, renaming if not unique
86+ *
87+ * @return possibly-renamed filename
88+ *
89+ * @throws OutOfMemoryError if the file could not be copied to a contiguous block of memory (or is >= 2GB)
90+ */
91+ @LibAnkiAlias(" write_data" )
92+ fun writeData (
93+ desiredFname : String ,
94+ data : ByteString ,
95+ ): String =
96+ col.backend.addMediaFile(
97+ desiredName = desiredFname,
98+ data = data,
99+ )
100+
76101 /*
77102 * String manipulation
78103 * ***********************************************************
0 commit comments