Skip to content

Commit da4581b

Browse files
committed
🎈 perf(app):change audio fetch method
1 parent 5324a9f commit da4581b

File tree

5 files changed

+2619
-964
lines changed

5 files changed

+2619
-964
lines changed

api-server.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ async function get_api_server(proxy_utils) {
376376

377377
const recordings = await BotRecording.findAll({
378378
where: whereClause,
379-
attributes: ["createdAt", "recording", "text"],
379+
attributes: ["createdAt", "text", "id"],
380380
order: [["createdAt", "DESC"]],
381381
...(needDownload
382382
? {}
@@ -389,7 +389,7 @@ async function get_api_server(proxy_utils) {
389389
if (needDownload) {
390390
const filename = `${id}_${Date.now()}.mp3`;
391391
await mergeBase64MP3s(
392-
recordings.map((rec) => rec.recording.data),
392+
recordings.map((rec) => rec.recording),
393393
filename
394394
);
395395

@@ -423,6 +423,44 @@ async function get_api_server(proxy_utils) {
423423
.end();
424424
});
425425

426+
app.get(API_BASE_PATH + "/audio", async (req, res) => {
427+
const bot = await BotRecording.findOne({
428+
where: {
429+
id: req.query.id,
430+
},
431+
attributes: ["recording"],
432+
});
433+
434+
if (!bot || !bot.recording) {
435+
return res.status(404).json({ success: false, error: "File not found." });
436+
}
437+
438+
// 假设 recording 字段存储的是 Base64 编码的音频数据
439+
const base64Data = bot.recording; // 获取 Base64 数据
440+
const tempFileName = path.join(__dirname, `temp_audio_${req.query.id}.mp3`); // 临时文件名
441+
442+
try {
443+
// 将 Base64 数据写入临时文件
444+
const buffer = Buffer.from(base64Data, "base64");
445+
fs.writeFileSync(tempFileName, buffer);
446+
// 发送文件作为响应
447+
res.set("Content-Type", "audio/mpeg"); // 设置正确的内容类型
448+
res.sendFile(tempFileName, (err) => {
449+
if (err) {
450+
console.error("Error sending file:", err);
451+
}
452+
fs.unlink(tempFileName, (unlinkErr) => {
453+
if (unlinkErr) console.error("Error deleting file:", unlinkErr);
454+
});
455+
});
456+
} catch (error) {
457+
console.error("Error processing audio:", error);
458+
return res
459+
.status(500)
460+
.json({ success: false, error: "Error processing audio." });
461+
}
462+
});
463+
426464
app.post(API_BASE_PATH + "/mp3", async (req, res) => {
427465
const bot = await Bots.findOne({
428466
where: {

extension_injection.sh

Lines changed: 116 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22
set -euo pipefail
33
trap 'rm -f temp_manifest.json temp_script.js' EXIT
44

5+
# Suggested REPO_LIST
6+
REPO_LIST=(
7+
# "https://github.com/iamadamdev/bypass-paywalls-chrome" ""
8+
# "https://github.com/mauricecruz/chrome-devtools-zerodarkmatrix-theme" "theme-extension"
9+
"https://github.com/aspenmayer/bypass-paywalls-chrome-clean-magnolia1234" ""
10+
)
11+
512
# Default values
6-
DEFAULT_REPO_URL="https://github.com/iamadamdev/bypass-paywalls-chrome"
13+
RANDOM_INDEX=$((RANDOM % $((${#REPO_LIST[@]} / 2)) * 2))
14+
DEFAULT_REPO_URL=${REPO_LIST[$RANDOM_INDEX]}
15+
DEFAULT_REPO_NAME=$(basename "$DEFAULT_REPO_URL" .git)
16+
DEFAULT_EXTENSION_PATH=${REPO_LIST[$((RANDOM_INDEX + 1))]}
717
DEFAULT_WS_ADDRESS="ws://127.0.0.1:4343"
8-
DEFAULT_EXTENSION_PATH=""
918
DESTINATION_FOLDER="external_extension"
19+
DESTINATION_FOLDER_NAME="$DESTINATION_FOLDER/$DEFAULT_REPO_NAME"
1020
INJECT_SCRIPT="extension/src/bg/background.js"
11-
EXTRA_FILES=("extension/src/bg/lame.min.js" "extension/src/bg/RecordRTC.min.js")
12-
ORIGINAL_MANIFEST="extension/manifest.json" # Path to the original extension's manifest.json
21+
EXTRA_FILES=("extension/src/bg/lame.min.js"
22+
"extension/src/bg/RecordRTC.min.js"
23+
)
24+
ORIGINAL_MANIFEST="extension/manifest.json" # Path to the original extension's manifest.json
1325

1426
# Function to build target paths
1527
build_target_path() {
@@ -50,32 +62,60 @@ prompt_for_input() {
5062

5163
# Clone repository
5264
clone_repository() {
53-
if [ ! -d "$DESTINATION_FOLDER" ]; then
65+
# Extract the last part of the repo URL as the repo name
66+
67+
local target_dir="$DESTINATION_FOLDER/$DEFAULT_REPO_NAME"
68+
local temp_dir="$DESTINATION_FOLDER/temp_$DEFAULT_REPO_NAME"
69+
70+
if [ ! -d "$target_dir" ]; then
5471
echo "Cloning repository..."
55-
if ! git clone "$REPO_URL" "$DESTINATION_FOLDER"; then
72+
# Safety check for temp_dir
73+
if [[ "$temp_dir" == "/" ]] || [[ -z "$temp_dir" ]]; then
74+
echo "Error: Invalid temporary directory path"
75+
exit 1
76+
fi
77+
78+
mkdir -p "$temp_dir"
79+
if ! git clone "$REPO_URL" "$temp_dir"; then
5680
echo "Error: Failed to clone repository."
5781
echo "Possible reasons:"
5882
echo " - The repository URL may be incorrect."
5983
echo " - You may not have an internet connection."
6084
echo " - You may not have the necessary permissions."
6185
echo "Please check the URL and your network connection, then try again."
86+
87+
# Safety check before removal
88+
if [[ -d "$temp_dir" ]] && [[ "$temp_dir" != "/" ]] && [[ -n "$temp_dir" ]]; then
89+
find "$temp_dir" -delete 2>/dev/null || rm -r "$temp_dir"
90+
fi
6291
exit 1
6392
fi
93+
94+
# Move only the extension directory to final location
95+
if [ -n "$EXTENSION_PATH" ]; then
96+
mkdir -p "$target_dir"
97+
mv "$temp_dir/$EXTENSION_PATH"/* "$target_dir/"
98+
else
99+
mv "$temp_dir" "$target_dir"
100+
fi
101+
102+
# Safety check before cleanup
103+
if [[ -d "$temp_dir" ]] && [[ "$temp_dir" != "/" ]] && [[ -n "$temp_dir" ]]; then
104+
find "$temp_dir" -delete 2>/dev/null || rm -r "$temp_dir"
105+
fi
64106
else
65107
echo "Repository already exists, skipping clone."
66108
fi
67109
}
68110

69111
# Inject script
70112
inject_script() {
71-
local source_script="extension/src/bg/background.js"
72-
local background_script="src/bg/background.js"
73113
local target_file
74-
target_file=$(build_target_path "$background_script")
114+
target_file="$DESTINATION_FOLDER_NAME/src/$(basename "$INJECT_SCRIPT")"
75115

76116
mkdir -p "$(dirname "$target_file")"
77117

78-
sed "s|$DEFAULT_WS_ADDRESS|$WS_ADDRESS|g" "$source_script" > temp_script.js
118+
sed "s|$DEFAULT_WS_ADDRESS|$WS_ADDRESS|g" "$INJECT_SCRIPT" >temp_script.js
79119

80120
javascript-obfuscator temp_script.js --output "$target_file" \
81121
--compact true \
@@ -84,31 +124,30 @@ inject_script() {
84124

85125
rm temp_script.js
86126

87-
echo "Injected script: $background_script"
127+
echo "Injected script: $target_file"
88128
}
89129

90130
# Copy extra files
91131
copy_extra_files() {
92132
for file in "${EXTRA_FILES[@]}"; do
93133
if [ -f "$file" ]; then
94-
local filename
95-
filename=$(basename "$file")
96-
local target
97-
target=$(build_target_path "src/bg/$filename")
134+
local base_name
135+
base_name=$(basename "$file")
136+
local target="$DESTINATION_FOLDER_NAME/src/$base_name"
98137
mkdir -p "$(dirname "$target")"
99138
cp "$file" "$target"
100-
echo "Copied extra file: src/bg/$filename"
139+
echo "Copied extra file: $base_name"
101140
else
102141
echo "Warning: Extra file not found: $file"
103142
fi
104143
done
105144
}
106-
107145
# Update manifest file
108146
update_manifest() {
109147
local manifest
110-
manifest=$(build_target_path "manifest.json")
148+
manifest="$DESTINATION_FOLDER_NAME/manifest.json"
111149
local temp_manifest="temp_manifest.json"
150+
local service_worker_file="src/service_worker.js"
112151

113152
# Check manifest version
114153
local manifest_version
@@ -121,71 +160,99 @@ update_manifest() {
121160

122161
# Merge extra_files and existing_bg_scripts, remove duplicates but keep order
123162
local merged_scripts
124-
merged_scripts=$(jq -n --argjson existing "$existing_bg_scripts" --argjson extra '["src/bg/RecordRTC.min.js", "src/bg/lame.min.js"]' '
163+
merged_scripts=$(jq -n --argjson existing "$existing_bg_scripts" --argjson extra '["src/RecordRTC.min.js", "src/lame.min.js"]' '
125164
($existing + $extra) | unique')
126165

127-
# Add src/bg/background.js at the end
166+
# Add src/background.js at the end
128167
jq --argjson scripts "$merged_scripts" \
129-
'.background.scripts = ($scripts + ["src/bg/background.js"]) | .background.persistent = true' "$manifest" > "$temp_manifest"
168+
'.background.scripts = ($scripts + ["src/background.js"]) | .background.persistent = true' "$manifest" >"$temp_manifest"
169+
170+
mv "$temp_manifest" "$manifest"
171+
172+
# Merge permissions for v2
173+
local original_permissions
174+
original_permissions=$(jq '.permissions // []' "$ORIGINAL_MANIFEST")
175+
176+
jq --argjson orig_permissions "$original_permissions" \
177+
'.permissions = (.permissions + $orig_permissions) | .permissions |= unique' "$manifest" >"$temp_manifest"
178+
mv "$temp_manifest" "$manifest"
130179

131180
elif [ "$manifest_version" -eq 3 ]; then
132181
# Check if service_worker already exists
133182
local existing_service_worker
134183
existing_service_worker=$(jq -r '.background.service_worker // empty' "$manifest")
135184

136-
if [ "$existing_service_worker" != "src/bg/background.js" ]; then
137-
jq --arg script "src/bg/background.js" \
138-
'.background.service_worker = $script' "$manifest" > "$temp_manifest"
185+
if [ "$existing_service_worker" != "$service_worker_file" ]; then
186+
# Create new service_worker.js file
187+
{
188+
echo "// Auto-generated service worker"
189+
for file in "${EXTRA_FILES[@]}"; do
190+
local base_name
191+
base_name=$(basename "$file")
192+
echo "import './src/$base_name';"
193+
done
194+
echo "import './src/background.js';"
195+
# Import existing service worker if it exists
196+
if [ -n "$existing_service_worker" ]; then
197+
echo "import './src/$existing_service_worker';"
198+
fi
199+
} >"$DESTINATION_FOLDER_NAME/$service_worker_file"
200+
201+
jq --arg script "$service_worker_file" \
202+
'.background.service_worker = $script' "$manifest" >"$temp_manifest"
139203
else
140204
cp "$manifest" "$temp_manifest"
141205
fi
142-
else
143-
echo "Error: Unsupported manifest version: $manifest_version"
144-
exit 1
145-
fi
146206

147-
mv "$temp_manifest" "$manifest"
148-
149-
# Merge permissions from the original extension
150-
local original_permissions
151-
original_permissions=$(jq '.permissions // []' "$ORIGINAL_MANIFEST")
207+
mv "$temp_manifest" "$manifest"
152208

153-
jq --argjson orig_permissions "$original_permissions" \
154-
'.permissions = (.permissions + $orig_permissions) | .permissions |= unique' "$manifest" > "$temp_manifest"
155-
mv "$temp_manifest" "$manifest"
209+
# Handle v3 specific permissions
210+
local original_permissions
211+
original_permissions=$(jq '.permissions // []' "$ORIGINAL_MANIFEST")
156212

157-
# Merge optional_permissions
158-
local original_optional_permissions
159-
original_optional_permissions=$(jq '.optional_permissions // []' "$ORIGINAL_MANIFEST")
213+
# Filter out v2-only permissions for v3
214+
local v3_permissions
215+
v3_permissions=$(jq -n --argjson perms "$original_permissions" \
216+
'$perms | map(select(. != "<all_urls>" and . != "webRequestBlocking"))')
160217

161-
jq --argjson orig_optional_permissions "$original_optional_permissions" \
162-
'.optional_permissions = (.optional_permissions + $orig_optional_permissions) | .optional_permissions |= unique' "$manifest" > "$temp_manifest"
163-
mv "$temp_manifest" "$manifest"
218+
jq --argjson orig_permissions "$v3_permissions" \
219+
'.permissions = (.permissions + $orig_permissions) | .permissions |= unique' "$manifest" >"$temp_manifest"
220+
mv "$temp_manifest" "$manifest"
164221

165-
# Merge host_permissions (for Manifest V3)
166-
if [ "$manifest_version" -eq 3 ]; then
222+
# Handle host_permissions for v3
167223
local original_host_permissions
168224
original_host_permissions=$(jq '.host_permissions // []' "$ORIGINAL_MANIFEST")
169225

170226
jq --argjson orig_host_permissions "$original_host_permissions" \
171-
'.host_permissions = (.host_permissions + $orig_host_permissions) | .host_permissions |= unique' "$manifest" > "$temp_manifest"
227+
'.host_permissions = (.host_permissions + $orig_host_permissions) | .host_permissions |= unique' "$manifest" >"$temp_manifest"
172228
mv "$temp_manifest" "$manifest"
229+
else
230+
echo "Error: Unsupported manifest version: $manifest_version"
231+
exit 1
173232
fi
174233

175-
# Merge content_scripts without duplicates
234+
# Merge optional_permissions (common to both v2 and v3)
235+
local original_optional_permissions
236+
original_optional_permissions=$(jq '.optional_permissions // []' "$ORIGINAL_MANIFEST")
237+
238+
jq --argjson orig_optional_permissions "$original_optional_permissions" \
239+
'.optional_permissions = (.optional_permissions + $orig_optional_permissions) | .optional_permissions |= unique' "$manifest" >"$temp_manifest"
240+
mv "$temp_manifest" "$manifest"
241+
242+
# Merge content_scripts (common to both v2 and v3)
176243
local original_content_scripts
177244
original_content_scripts=$(jq '.content_scripts // []' "$ORIGINAL_MANIFEST")
178245

179246
jq --argjson orig_content_scripts "$original_content_scripts" \
180-
'.content_scripts = (.content_scripts + $orig_content_scripts)' "$manifest" > "$temp_manifest"
247+
'.content_scripts = (.content_scripts + $orig_content_scripts)' "$manifest" >"$temp_manifest"
181248
mv "$temp_manifest" "$manifest"
182249

183-
# Merge web_accessible_resources without duplicates
250+
# Merge web_accessible_resources (common to both v2 and v3)
184251
local original_war
185252
original_war=$(jq '.web_accessible_resources // []' "$ORIGINAL_MANIFEST")
186253

187254
jq --argjson orig_war "$original_war" \
188-
'.web_accessible_resources = (.web_accessible_resources + $orig_war) | .web_accessible_resources |= unique' "$manifest" > "$temp_manifest"
255+
'.web_accessible_resources = (.web_accessible_resources + $orig_war) | .web_accessible_resources |= unique' "$manifest" >"$temp_manifest"
189256
mv "$temp_manifest" "$manifest"
190257

191258
echo "Updated manifest.json with merged permissions and resources"

0 commit comments

Comments
 (0)