Skip to content

Commit

Permalink
handle case where the proof dir is accidentally missing or null
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Apr 12, 2024
1 parent c768a8a commit df5722f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.net.Uri
import androidx.core.net.toFile
import org.witness.proofmode.ProofMode
import org.witness.proofmode.service.MediaWatcher
import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
Expand Down Expand Up @@ -53,13 +54,16 @@ public class DefaultStorageProvider (context : Context) : StorageProvider {

}

override fun getInputStream(hash: String?, identifier: String?): InputStream {
override fun getInputStream(hash: String?, identifier: String?): InputStream? {
val file = File(
getHashStorageDir(
hash!!
), identifier!!
)
return FileInputStream(file)
if (file.exists())
return FileInputStream(file)
else
return null
}

override fun getOutputStream(hash: String?, identifier: String?): OutputStream {
Expand Down Expand Up @@ -106,7 +110,13 @@ public class DefaultStorageProvider (context : Context) : StorageProvider {
val dirProof = hash?.let { getHashStorageDir(it) }
if (dirProof?.exists() == true)
{
return (identifier?.let { File(dirProof, it).exists() } == true)
return (identifier?.let {
val dirProofHash = File(dirProof, it)
dirProofHash.listFiles()?.forEach { Timber.d(it.absolutePath)}
dirProofHash.exists()
} == true)


}

return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,30 @@ class ProofModeUtil {

fun getProofHashMap (storageProvider: StorageProvider, proofHash: String, context: Context): HashMap<String,String>? {

val hmap = HashMap<String,String>()
val hashMapProof = HashMap<String,String>()
val identifier = proofHash+ProofMode.PROOF_FILE_JSON_TAG

if (storageProvider.proofIdentifierExists(proofHash,identifier))
{
val jsonData = BufferedReader(InputStreamReader(storageProvider.getInputStream(proofHash,identifier))).readText();
val jsonObject = JSONObject(jsonData)
val itKeys = jsonObject.keys()
while (itKeys.hasNext())
{
while (itKeys.hasNext())
{
val key = itKeys.next()
val value = jsonObject.getString(key)
if (value.isNotEmpty()) {
hmap[key] = value
val proofStream = storageProvider.getInputStream(proofHash,identifier)
proofStream.let {
val jsonData = BufferedReader(InputStreamReader(proofStream)).readText();
val jsonObject = JSONObject(jsonData)
val itKeys = jsonObject.keys()
while (itKeys.hasNext()) {
while (itKeys.hasNext()) {
val key = itKeys.next()
val value = jsonObject.getString(key)
if (value.isNotEmpty()) {
hashMapProof[key] = value
}
}
}
}

}

return hmap
return hashMapProof
}
}
}

0 comments on commit df5722f

Please sign in to comment.