/
DefaultStorageResolver.kt
61 lines (51 loc) · 1.91 KB
/
DefaultStorageResolver.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.tonyodev.fetch2core
import android.content.Context
import java.io.FileNotFoundException
/** The default Storage Resolver used by Fetch. Extend this class if you want to provide your
* own implementation.*/
open class DefaultStorageResolver(
/* Context*/
protected val context: Context,
/**The default temp directory used by Fetch for Parallel Downloaders.*/
protected val defaultTempDir: String) : StorageResolver {
override fun createFile(file: String, increment: Boolean): String {
return createFileAtPath(file, increment, context)
}
override fun deleteFile(file: String): Boolean {
return deleteFile(file, context)
}
override fun getRequestOutputResourceWrapper(request: Downloader.ServerRequest): OutputResourceWrapper {
return getOutputResourceWrapper(request.file, context.contentResolver)
}
override fun getDirectoryForFileDownloaderTypeParallel(request: Downloader.ServerRequest): String {
return defaultTempDir
}
override fun fileExists(file: String): Boolean {
if (file.isEmpty()) {
return false
}
return try {
val outputResourceWrapper = getOutputResourceWrapper(file, context.contentResolver)
outputResourceWrapper.close()
true
} catch (e: Exception) {
false
}
}
override fun renameFile(oldFile: String, newFile: String): Boolean {
if (oldFile.isEmpty() || newFile.isEmpty()) {
return false
}
return renameFile(oldFile, newFile, context)
}
override fun preAllocateFile(file: String, contentLength: Long): Boolean {
if (file.isEmpty()) {
throw FileNotFoundException("$file $FILE_NOT_FOUND")
}
if (contentLength < 1) {
return true
}
allocateFile(file, contentLength, context)
return true
}
}