Add dry delete API

pull/653/merge
Varun Patil 2023-09-30 13:39:11 -07:00
parent 8976b53284
commit 9f466511eb
2 changed files with 19 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse import android.webkit.WebResourceResponse
import android.widget.Toast import android.widget.Toast
import androidx.media3.common.util.UnstableApi
import gallery.memories.mapper.SystemImage import gallery.memories.mapper.SystemImage
import gallery.memories.service.AccountService import gallery.memories.service.AccountService
import gallery.memories.service.DownloadService import gallery.memories.service.DownloadService
@ -16,7 +17,7 @@ import org.json.JSONArray
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.net.URLDecoder import java.net.URLDecoder
class NativeX(private val mCtx: MainActivity) { @UnstableApi class NativeX(private val mCtx: MainActivity) {
val TAG = NativeX::class.java.simpleName val TAG = NativeX::class.java.simpleName
private var themeStored = false private var themeStored = false
@ -60,7 +61,7 @@ class NativeX(private val mCtx: MainActivity) {
val response = try { val response = try {
when (request.method) { when (request.method) {
"GET" -> { "GET" -> {
routerGet(path) routerGet(request)
} }
"OPTIONS" -> { "OPTIONS" -> {
WebResourceResponse("text/plain", "UTF-8", ByteArrayInputStream("".toByteArray())) WebResourceResponse("text/plain", "UTF-8", ByteArrayInputStream("".toByteArray()))
@ -191,7 +192,9 @@ class NativeX(private val mCtx: MainActivity) {
} }
@Throws(Exception::class) @Throws(Exception::class)
private fun routerGet(path: String): WebResourceResponse { private fun routerGet(request: WebResourceRequest): WebResourceResponse {
val path = request.url.path ?: return makeErrorResponse()
val parts = path.split("/").toTypedArray() val parts = path.split("/").toTypedArray()
return if (path.matches(API.DAYS)) { return if (path.matches(API.DAYS)) {
makeResponse(query.getDays()) makeResponse(query.getDays())
@ -200,7 +203,7 @@ class NativeX(private val mCtx: MainActivity) {
} else if (path.matches(API.IMAGE_INFO)) { } else if (path.matches(API.IMAGE_INFO)) {
makeResponse(query.getImageInfo(parts[4].toLong())) makeResponse(query.getImageInfo(parts[4].toLong()))
} else if (path.matches(API.IMAGE_DELETE)) { } else if (path.matches(API.IMAGE_DELETE)) {
makeResponse(query.delete(parseIds(parts[4]))) makeResponse(query.delete(parseIds(parts[4]), request.url.getBooleanQueryParameter("dry", false)))
} else if (path.matches(API.IMAGE_PREVIEW)) { } else if (path.matches(API.IMAGE_PREVIEW)) {
makeResponse(image.getPreview(parts[3].toLong()), "image/jpeg") makeResponse(image.getPreview(parts[3].toLong()), "image/jpeg")
} else if (path.matches(API.IMAGE_FULL)) { } else if (path.matches(API.IMAGE_FULL)) {

View File

@ -171,7 +171,7 @@ class TimelineQuery(private val mCtx: MainActivity) {
} }
@Throws(Exception::class) @Throws(Exception::class)
fun delete(auids: List<Long>): JSONObject { fun delete(auids: List<Long>, dry: Boolean): JSONObject {
synchronized(this) { synchronized(this) {
if (deleting) { if (deleting) {
throw Exception("Already deleting another set of images") throw Exception("Already deleting another set of images")
@ -179,10 +179,19 @@ class TimelineQuery(private val mCtx: MainActivity) {
deleting = true deleting = true
} }
val response = Response.OK
try { try {
// Get list of file IDs // Get list of file IDs
val photos = mPhotoDao.getPhotosByAUIDs(auids) val photos = mPhotoDao.getPhotosByAUIDs(auids)
if (photos.isEmpty()) return Response.OK
// Let the UI know how many files we are deleting
response.put("count", photos.size)
// Let the UI know if we are going to ask for confirmation
response.put("confirms", Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
if (dry || photos.isEmpty()) return response
val fileIds = photos.map { it.localId } val fileIds = photos.map { it.localId }
// List of URIs // List of URIs
@ -222,7 +231,7 @@ class TimelineQuery(private val mCtx: MainActivity) {
synchronized(this) { deleting = false } synchronized(this) { deleting = false }
} }
return Response.OK return response
} }
private fun syncDb(startTime: Long): Int { private fun syncDb(startTime: Long): Int {