Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 1c7f7ec

Browse files
committed
wip
1 parent a6f3881 commit 1c7f7ec

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

process_files.sh

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/bin/bash
2+
3+
if [ "$#" -ne 2 ]; then
4+
echo "Usage: $0 <source_directory> <output_directory>"
5+
echo "Example: $0 ./my_files ./output"
6+
exit 1
7+
fi
8+
9+
SOURCE_DIR=$(realpath "$1")
10+
OUTPUT_DIR=$(realpath "$2")
11+
INFO_JSON="$OUTPUT_DIR/info.json"
12+
13+
echo "Processing files from: $SOURCE_DIR"
14+
echo "Output directory: $OUTPUT_DIR"
15+
echo ""
16+
17+
mkdir -p "$OUTPUT_DIR"
18+
19+
PROCESSED_HASHES=$(mktemp)
20+
JSON_CONTENT=$(mktemp)
21+
DELETE_LIST=$(mktemp)
22+
PROCESSED_PATHS=$(mktemp)
23+
trap 'rm -f "$PROCESSED_HASHES" "$JSON_CONTENT" "$DELETE_LIST" "$PROCESSED_PATHS"' EXIT
24+
25+
if [ -f "$INFO_JSON" ]; then
26+
echo "Found existing info.json, using as base..."
27+
cp "$INFO_JSON" "$JSON_CONTENT"
28+
else
29+
echo "Creating new info.json..."
30+
echo '{"delete_list":[],"files":[]}' > "$JSON_CONTENT"
31+
fi
32+
33+
cd "$SOURCE_DIR" || exit 1
34+
35+
echo "Finding and processing files..."
36+
find . -type f ! -path "$OUTPUT_DIR/*" -print0 | \
37+
while IFS= read -r -d $'\0' file; do
38+
echo " Processing: $file"
39+
rel_path="${file#./}"
40+
echo "$rel_path" >> "$PROCESSED_PATHS"
41+
current_hash=$(b3sum "$file" | cut -d ' ' -f 1)
42+
size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file")
43+
44+
existing_entry=$(jq -r --arg path "$rel_path" '.files[] | select(.name == $path)' "$JSON_CONTENT")
45+
46+
if [ -n "$existing_entry" ]; then
47+
old_hash=$(echo "$existing_entry" | jq -r '.hash')
48+
if [ "$current_hash" = "$old_hash" ] && [ -f "$OUTPUT_DIR/$old_hash" ]; then
49+
echo " Unchanged, skipping..."
50+
echo "$old_hash" >> "$PROCESSED_HASHES"
51+
continue
52+
else
53+
echo " Changed, updating..."
54+
echo "$old_hash" >> "$DELETE_LIST"
55+
fi
56+
else
57+
echo " New file..."
58+
fi
59+
60+
cp "$file" "$OUTPUT_DIR/$current_hash"
61+
echo "$current_hash" >> "$PROCESSED_HASHES"
62+
63+
jq --arg path "$rel_path" 'del(.files[] | select(.name == $path))' "$JSON_CONTENT" > "$JSON_CONTENT.tmp" && mv "$JSON_CONTENT.tmp" "$JSON_CONTENT"
64+
65+
jq --arg path "$rel_path" \
66+
--arg size "$size" \
67+
--arg hash "$current_hash" \
68+
'.files += [{"name": $path, "size": ($size|tonumber), "hash": $hash}]' \
69+
"$JSON_CONTENT" > "$JSON_CONTENT.tmp" && mv "$JSON_CONTENT.tmp" "$JSON_CONTENT"
70+
done
71+
72+
echo "Removing entries for deleted files..."
73+
while IFS= read -r entry; do
74+
path=$(echo "$entry" | jq -r '.name')
75+
hash=$(echo "$entry" | jq -r '.hash')
76+
if ! grep -Fxq "$path" "$PROCESSED_PATHS"; then
77+
echo " Removing entry for deleted file: $path"
78+
jq --arg path "$path" 'del(.files[] | select(.name == $path))' "$JSON_CONTENT" > "$JSON_CONTENT.tmp" && mv "$JSON_CONTENT.tmp" "$JSON_CONTENT"
79+
if [ -f "$OUTPUT_DIR/$hash" ]; then
80+
echo " Marking for deletion: $hash"
81+
echo "$hash" >> "$DELETE_LIST"
82+
fi
83+
fi
84+
done < <(jq -c '.files[]' "$JSON_CONTENT")
85+
86+
echo "Writing info.json..."
87+
cp "$JSON_CONTENT" "$INFO_JSON"
88+
89+
echo "Cleaning up unused files..."
90+
for file in "$OUTPUT_DIR"/*; do
91+
if [ "$file" = "$INFO_JSON" ]; then
92+
continue
93+
fi
94+
if [ ! -f "$file" ]; then
95+
continue
96+
fi
97+
basename=$(basename "$file")
98+
if ! grep -q "^${basename}$" "$PROCESSED_HASHES"; then
99+
echo " Removing unused file: $basename"
100+
rm "$file"
101+
fi
102+
done
103+
104+
echo "Removing old and deleted files..."
105+
sort -u "$DELETE_LIST" | while read -r hash; do
106+
if [ -f "$OUTPUT_DIR/$hash" ]; then
107+
echo " Removing: $hash"
108+
rm "$OUTPUT_DIR/$hash"
109+
fi
110+
done
111+
112+
echo ""
113+
echo "Done! Files processed and info.json updated."

src/cdn.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ impl File {
1212
format!("{}/{}", crate::global::CDN_URL, self.hash)
1313
}
1414

15+
pub fn cache_name(&self) -> String {
16+
format!("{}", self.hash[..24].to_string())
17+
}
18+
1519
pub fn cache_path(&self) -> String {
16-
format!("{}/{}", crate::global::CACHE_DIR, self.hash)
20+
format!("{}/{}", crate::global::CACHE_DIR, self.cache_name())
1721
}
1822

1923
pub fn size_human(&self) -> String {
@@ -27,8 +31,8 @@ pub struct Info {
2731
pub files: Vec<File>,
2832
}
2933

30-
pub async fn get_info(url: &str) -> Result<Info, Box<dyn std::error::Error>> {
31-
let info = crate::http::quick_request(url).await?;
34+
pub async fn get_info() -> Result<Info, Box<dyn std::error::Error>> {
35+
let info = crate::http::quick_request(&format!("{0}/info.json", crate::global::CDN_URL)).await?;
3236
Ok(serde_json::from_str(&info)?)
3337
}
3438

src/global.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ pub const GITHUB_OWNER: &str = "mxve";
99
/// The repository of the launcher
1010
pub const GITHUB_REPO: &str = "alterware-launcher";
1111

12+
13+
// TODO: Make this configurable
1214
/// Base URL for file downloads
13-
pub const CDN_URL: &str = "https://cdn.getserve.rs";
15+
pub const CDN_URL: &str = "https://cdn.getserve.rs/stable";
1416

17+
// TODO: Make this configurable
1518
/// The path to the download cache
1619
pub const CACHE_DIR: &str = "awtmp";
1720

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1717
let clients = game.clients();
1818
println!("Clients: {:?}", clients);
1919

20-
let info = cdn::get_info(format!("{}/info.json", global::CDN_URL).as_str()).await?;
20+
let info = cdn::get_info().await?;
2121
let files = cdn::filter_files(info.files.clone(), game);
2222
println!("Files: {:?}", files);
2323
for file in files {
2424
println!("File: {:?}", file);
2525
println!("Size: {:?}", file.size_human());
2626
println!("URL: {:?}", file.url());
27+
println!("Cache name: {:?}", file.cache_name());
28+
println!("Cache path: {:?}", file.cache_path());
2729
}
2830
} else {
2931
println!("No game detected");

0 commit comments

Comments
 (0)