From 34255218731730990bb518bb47fc44286ee71b32 Mon Sep 17 00:00:00 2001 From: uintdev Date: Tue, 16 Jul 2024 18:04:29 +0100 Subject: [PATCH] Add flatpak support under a flag --- README.md | 1 + go.mod | 5 ++++- go.sum | 2 ++ main.go | 28 +++++++++++++++++++++++----- utils/process.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c518448..fbd90b7 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Discord Cache Dump is a tool that gathers the cache of all known Electron Discor ## Features - Detection of known Discord build types (can do multiple in a single run) +- Flatpak support using `--flatpak` (or `-f`) flag (i.e. `./dcd --flatpak`) - Discloses count of amount of files it is unable to gather at the time for that particular build - Supports Windows, GNU/Linux, and macOS - Checks storage available where the program is being ran before copying diff --git a/go.mod b/go.mod index 5c9b6c7..6b6e7dd 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,7 @@ require ( github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 ) -require golang.org/x/sys v0.1.0 // indirect +require ( + golang.org/x/sys v0.1.0 // indirect + golang.org/x/text v0.16.0 // indirect +) diff --git a/go.sum b/go.sum index ec79bfd..0110687 100644 --- a/go.sum +++ b/go.sum @@ -7,3 +7,5 @@ github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285/go.m golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= diff --git a/main.go b/main.go index e430327..c75c0ec 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( ) const ( - softVersion = "1.2.4" + softVersion = "1.3.0" dumpDir = "dump" ) @@ -75,6 +75,7 @@ func main() { var opts struct { Build string `short:"b" long:"build" description:"Select build type: stable, ptb, canary, development"` + Flatpak bool `short:"f" long:"flatpak" description:"Target flatpak builds (Linux only)"` Noninteractive bool `short:"n" long:"noninteractive" description:"Non-interactive -- no 'enter' key required"` } @@ -189,11 +190,20 @@ func main() { fmt.Scanln() } + flatpakMode := opts.Flatpak && platform == "linux" + if flatpakMode { + fmt.Print("Flatpak mode\n") + } + fmt.Print("Checking for existing cache directories ...\n\n") // Check if directories exist for i := 0; i < len(discordBuildDir); i++ { - filePath := fmt.Sprintf(cachePath[platform], homePath, discordBuildDir[i]) + filePathRaw := cachePath[platform] + if flatpakMode { + filePathRaw = DCDUtils.FlatpakPath(discordBuildDir[i]) + } + filePath := fmt.Sprintf(filePathRaw, homePath, discordBuildDir[i]) if _, err := os.Stat(filePath); !os.IsNotExist(err) { fmt.Printf("Found: Discord %s\n", discordBuildName[i]) pathStatus[i] = true @@ -205,7 +215,11 @@ func main() { // Check if the directories are empty and store names of cached files for i := 0; i < len(discordBuildDir); i++ { if pathStatus[i] { - filePath := fmt.Sprintf(cachePath[platform], homePath, discordBuildDir[i]) + filePathRaw := cachePath[platform] + if flatpakMode { + filePathRaw = DCDUtils.FlatpakPath(discordBuildDir[i]) + } + filePath := fmt.Sprintf(filePathRaw, homePath, discordBuildDir[i]) cacheListing, err := ioutil.ReadDir(filePath) if err != nil { fmt.Printf("[ERROR] Unable to read directory for Discord %s%s", discordBuildName[i], DCDUtils.ExitNewLine()) @@ -216,7 +230,7 @@ func main() { cachedFile[i] = make(map[int]string) for k, v := range cacheListing { cachedFile[i][k] = v.Name() - overallSize = DCDUtils.SizeStore(filePath + v.Name(), overallSize) + overallSize = DCDUtils.SizeStore(filePath+v.Name(), overallSize) } fmt.Printf("Discord %s :: found %d cached files\n", discordBuildName[i], len(cacheListing)) } else { @@ -286,7 +300,11 @@ func main() { fmt.Printf("Copying %d files from Discord %s ...\n", len(cachedFile[i]), discordBuildName[i]) for it := 0; it < len(cachedFile[i]); it++ { // Build the paths to use during the copy operation - fromPath := fmt.Sprintf(cachePath[platform], homePath, discordBuildDir[i]) + filePathRaw := cachePath[platform] + if flatpakMode { + filePathRaw = DCDUtils.FlatpakPath(discordBuildDir[i]) + } + fromPath := fmt.Sprintf(filePathRaw, homePath, discordBuildDir[i]) toPath := dumpDir + "/" + timeDateStamp + "/" + discordBuildName[i] + "/" + cachedFile[i][it] // Copying the files one-by-one unreadableRes = DCDUtils.CopyFile(fromPath+cachedFile[i][it], toPath, sudoerUID, unreadableRes) diff --git a/utils/process.go b/utils/process.go index bfca5f5..5766928 100644 --- a/utils/process.go +++ b/utils/process.go @@ -8,8 +8,38 @@ import ( "regexp" "runtime" "strconv" + "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) +// Formulate the end of the flatpak package name to then use as a normal path +func FlatpakPath(buildType string) string { + resultingBuildSegment := "" + splitBuildName := strings.Split(buildType, "discord") + if len(splitBuildName) > 1 { + if splitBuildName[1] != "" { + resultingBuildSegment = strings.Join([]string{"discord", splitBuildName[1]}, " ") + } else { + resultingBuildSegment = buildType + } + } else { + resultingBuildSegment = buildType + } + + titleCaser := cases.Title(language.English) + casedBuildName := titleCaser.String(resultingBuildSegment) + + packageNameSuffix := strings.Replace(casedBuildName, " ", "", -1) + + path := "%s/.var/app/com.discordapp.%s/config/%s/Cache/Cache_Data/" + path = fmt.Sprintf(path, "%s", packageNameSuffix, "%s") + + return path +} + + // Extract cache files (for GNU/Linux and macOS) func FileExtractor(contents []byte) []byte { var magicNumber string