-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_script2.py
More file actions
108 lines (98 loc) · 3.6 KB
/
Copy pathreplace_script2.py
File metadata and controls
108 lines (98 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sys
with open('tracker/templates/tracker/dashboard.html', 'r', encoding='utf-8') as f:
lines = f.readlines()
start = -1
end = -1
for i, line in enumerate(lines):
if 'function uploadNativeSnap()' in line:
start = i
if start != -1 and i > start and 'btn.disabled = false;' in line:
if '});' in lines[i+1] and '}' in lines[i+2]:
end = i + 2
break
if start != -1 and end != -1:
new_func = """function compressImage(file, maxWidth, maxHeight, quality, callback) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(event) {
const img = new Image();
img.src = event.target.result;
img.onload = function() {
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
canvas.toBlob(function(blob) {
callback(blob);
}, 'image/jpeg', quality);
};
};
}
function uploadNativeSnap() {
if (!selectedFile) {
alert("Please select a photo first.");
return;
}
const caption = document.getElementById('snapCaption').value.trim();
const btn = document.querySelector('button[onclick="uploadNativeSnap()"]') || document.querySelector('.snap-upload-btn');
const origText = btn ? btn.innerHTML : 'Save';
if(btn) {
btn.innerHTML = 'Optimizing... ⏳';
btn.disabled = true;
}
compressImage(selectedFile, 1200, 1200, 0.8, function(compressedBlob) {
const fd = new FormData();
fd.append('title', caption);
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
fd.append('date', `${yyyy}-${mm}-${dd}`);
fd.append('images', compressedBlob, selectedFile.name || 'snap.jpg');
fd.append('captions', caption);
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
if(btn) btn.innerHTML = 'Uploading... 🚀';
fetch('/dashboard/snap/upload/', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
'X-Requested-With': 'XMLHttpRequest'
},
body: fd
})
.then(res => res.json())
.then(data => {
if (data.status === 'success') {
location.reload();
} else {
alert("Error saving memory.");
if(btn) { btn.innerHTML = origText; btn.disabled = false; }
}
}).catch(err => {
console.error(err);
alert("Network error.");
if(btn) { btn.innerHTML = origText; btn.disabled = false; }
});
});
}
"""
lines = lines[:start] + [new_func] + lines[end+1:]
with open('tracker/templates/tracker/dashboard.html', 'w', encoding='utf-8') as f:
f.writelines(lines)
print("Successfully updated HTML via indices")
else:
print("Could not find bounds:", start, end)