Skip to content

Commit fd58566

Browse files
david-allisonmikehardy
authored andcommitted
1 parent 9d1e7f3 commit fd58566

File tree

1 file changed

+30
-5
lines changed
  • libanki/src/main/java/com/ichi2/anki/libanki

1 file changed

+30
-5
lines changed

libanki/src/main/java/com/ichi2/anki/libanki/Media.kt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package com.ichi2.anki.libanki
1919

2020
import androidx.annotation.WorkerThread
2121
import anki.media.CheckMediaResponse
22+
import com.google.protobuf.ByteString
2223
import com.google.protobuf.kotlin.toByteString
2324
import com.ichi2.anki.libanki.Media.Companion.htmlMediaRegexps
2425
import com.ichi2.anki.libanki.exception.EmptyMediaException
2526
import com.ichi2.anki.libanki.utils.LibAnkiAlias
2627
import com.ichi2.anki.libanki.utils.NotInLibAnki
28+
import net.ankiweb.rsdroid.RustCleanup
2729
import timber.log.Timber
2830
import 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

Comments
 (0)