Prism-File-Explorer icon indicating copy to clipboard operation
Prism-File-Explorer copied to clipboard

[Enhancement] not always nessasary ".prism" folder

Open CookieXD opened this issue 3 months ago • 1 comments

I and many others will never use the Recycling bin feature. It's not nessasary to recreate the .prism folder if nothing is send to the Recycling bin. Maybe only create the folder if something gets send there?

Also you might as well use the same method as the Google files app and others to make it intercompatible.

So changing the name from testfile.txt to .trashed-{unix-timestamp}-testfile.txt

For example .trashed-1760419789-testfile.txt

import java.io.File
import java.util.concurrent.TimeUnit

fun trashFile(file: File): File? {
    if (!file.exists()) return null
    val timestamp = System.currentTimeMillis() / 1000
    val trashedFile = File(file.parent, ".trashed-$timestamp-${file.name}")
    // Rename the file to ".trashed-<timestamp>-<originalName>"
    return if (file.renameTo(trashedFile)) trashedFile else null
}

fun restoreFile(trashedFile: File): File? {
    if (!trashedFile.exists() || !trashedFile.name.startsWith(".trashed-")) return null
    // Extract the original name (everything after the second dash)
    val originalName = trashedFile.name.split("-", limit = 3).last()
    val restoredFile = File(trashedFile.parent, originalName)
    // Rename back to original name
    return if (trashedFile.renameTo(restoredFile)) restoredFile else null
}

fun cleanupOldTrashedFiles(directory: File, days: Int = 30) {
    if (!directory.exists() || !directory.isDirectory) return
    val now = System.currentTimeMillis() / 1000
    val maxAgeSeconds = TimeUnit.DAYS.toSeconds(days.toLong())

    directory.listFiles()?.forEach { file ->
        if (file.name.startsWith(".trashed-")) {
            val parts = file.name.split("-")
            if (parts.size >= 3) {
                val timestamp = parts[1].toLongOrNull()
                if (timestamp != null && now - timestamp > maxAgeSeconds) {
                    file.delete()
                }
            }
        }
    }
}

// Example usage:
fun main() {
    val file = File("/storage/emulated/0/Download/brawlstars_errors.txt")

    // Move file to "trash" (rename it in place)
    val trashed = trashFile(file)
    println("File renamed to: ${trashed?.name}")

    // Clean up trashed files older than 30 days in the same folder
    cleanupOldTrashedFiles(File(file.parent))

    // Restore the file back to its original name
    trashed?.let {
        val restored = restoreFile(it)
        println("File restored to: ${restored?.name}")
    }
}

CookieXD avatar Sep 14 '25 05:09 CookieXD

Thanks for the suggestion. I'm not aware of how Google files does it but I'll consider that.

Raival-e avatar Sep 14 '25 06:09 Raival-e