Skip to content

Commit

Permalink
Add flatpak support under a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
uintdev committed Jul 16, 2024
1 parent 0a11505 commit 3425521
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
softVersion = "1.2.4"
softVersion = "1.3.0"
dumpDir = "dump"
)

Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions utils/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3425521

Please sign in to comment.