Skip to content

Commit

Permalink
Merge pull request #275 from 014967/fix/save_title
Browse files Browse the repository at this point in the history
[fix] add title parsing logic
  • Loading branch information
014967 authored Oct 12, 2023
2 parents 2f7e076 + 89d8010 commit e3c587d
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun saveBitmapToStorage(
val imageOutStream: OutputStream?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val values = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, title)
put(MediaStore.Images.Media.DISPLAY_NAME, parseTitle(title))
put(MediaStore.Images.Media.MIME_TYPE, mimeType)
put(MediaStore.Images.Media.RELATIVE_PATH, directory)
}
Expand All @@ -41,10 +41,16 @@ fun saveBitmapToStorage(
}
} else {
val imagePath = Environment.getExternalStoragePublicDirectory(directory).absolutePath
val image = File(imagePath, title)
val image = File(imagePath, parseTitle(title))
imageOutStream = FileOutputStream(image)
}
imageOutStream.use { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it) }.run {
onSuccessToSave()
}
}

private fun parseTitle(title: String): String {
val regex = Regex("^((http[s]?|ftp):/)?/?([^:/\\s]+)((/\\w+)*/)([\\w\\-.]+[^#?\\s]+)(.*)?(#[\\w\\-]+)?\$")
val matchResult = regex.find(title)
return matchResult?.groups?.get(6)?.value ?: System.currentTimeMillis().toString()
}

0 comments on commit e3c587d

Please sign in to comment.