Skip to content

Commit 2d448f5

Browse files
david-allisonmikehardy
authored andcommitted
refactor: importMediaToDirectory
* No callers catch exceptions * `EmptyMediaException` can't be thrown * we guard against `EFieldType.TEXT` earlier * `emptyFile.length == 0L` * `field` is always non-null
1 parent cde85c7 commit 2d448f5

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/servicelayer/NoteService.kt

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ import android.os.Bundle
2323
import androidx.annotation.CheckResult
2424
import androidx.annotation.VisibleForTesting
2525
import com.ichi2.anki.CollectionManager.withCol
26-
import com.ichi2.anki.CrashReportService
2726
import com.ichi2.anki.FieldEditText
2827
import com.ichi2.anki.libanki.Card
2928
import com.ichi2.anki.libanki.Collection
3029
import com.ichi2.anki.libanki.Note
3130
import com.ichi2.anki.libanki.NoteTypeId
3231
import com.ichi2.anki.libanki.NotetypeJson
3332
import com.ichi2.anki.libanki.QueueType
34-
import com.ichi2.anki.libanki.exception.EmptyMediaException
3533
import com.ichi2.anki.multimediacard.IMultimediaEditableNote
3634
import com.ichi2.anki.multimediacard.fields.AudioRecordingField
3735
import com.ichi2.anki.multimediacard.fields.EFieldType
@@ -44,7 +42,6 @@ import com.ichi2.anki.observability.undoableOp
4442
import org.json.JSONException
4543
import timber.log.Timber
4644
import java.io.File
47-
import java.io.IOException
4845

4946
object NoteService {
5047
/**
@@ -129,39 +126,23 @@ object NoteService {
129126
*/
130127
fun importMediaToDirectory(
131128
col: Collection,
132-
field: IField?,
129+
field: IField,
133130
) {
134-
var tmpMediaPath: File? = null
135-
when (field!!.type) {
136-
EFieldType.AUDIO_RECORDING, EFieldType.MEDIA_CLIP, EFieldType.IMAGE -> tmpMediaPath = field.mediaFile
137-
EFieldType.TEXT -> {
138-
}
139-
}
140-
if (tmpMediaPath != null) {
141-
try {
142-
val inFile = tmpMediaPath
143-
if (inFile.exists() && inFile.length() > 0) {
144-
val fname = col.media.addFile(inFile)
145-
val outFile = File(col.media.dir, fname)
146-
Timber.v("""File "%s" should be copied to "%s""", fname, outFile)
147-
if (field.hasTemporaryMedia && outFile != tmpMediaPath) {
148-
// Delete original
149-
inFile.delete()
150-
}
151-
when (field.type) {
152-
EFieldType.AUDIO_RECORDING, EFieldType.MEDIA_CLIP, EFieldType.IMAGE -> field.mediaFile = outFile
153-
else -> {
154-
}
155-
}
156-
}
157-
} catch (e: IOException) {
158-
throw RuntimeException(e)
159-
} catch (mediaException: EmptyMediaException) {
160-
// This shouldn't happen, but we're fine to ignore it if it does.
161-
Timber.w(mediaException)
162-
CrashReportService.sendExceptionReport(mediaException, "noteService::importMediaToDirectory")
163-
}
131+
val inFile =
132+
when (field.type) {
133+
EFieldType.AUDIO_RECORDING, EFieldType.MEDIA_CLIP, EFieldType.IMAGE -> field.mediaFile
134+
EFieldType.TEXT -> null
135+
} ?: return
136+
137+
if (inFile.length() == 0L) return
138+
139+
val fname = col.media.addFile(inFile)
140+
val outFile = File(col.media.dir, fname)
141+
Timber.v("""File "%s" should be copied to "%s""", fname, outFile)
142+
if (field.hasTemporaryMedia && outFile != inFile) {
143+
inFile.delete()
164144
}
145+
field.mediaFile = outFile
165146
}
166147

167148
/**

0 commit comments

Comments
 (0)