import os import shutil import subprocess import urllib.request BUCKET_PATH = "/var/lib/docker/volumes/sss6crntvrcs54qbhvhy5728_appwrite-uploads/_data/app-69b84df5003bd8957c5c/media_bucket" FILE_ID = "6a37df0e00089708650b" TEST_FILE = f"{FILE_ID}.jpg" target_file = os.path.join(BUCKET_PATH, TEST_FILE) backup_file = f"{target_file}.bak" tmp_opt = f"/tmp/{TEST_FILE}" if not os.path.exists(tmp_opt): print("Error: /tmp optimized file not found") exit(1) new_size = os.path.getsize(tmp_opt) new_md5 = "e0a0fba38f6245a623eefc1a001852cc" print("1. Creating backup of original...") shutil.copy2(target_file, backup_file) print("2. Replacing target file with optimized file...") shutil.copy2(tmp_opt, target_file) # Ensure proper permissions and ownership os.chmod(target_file, 0o644) shutil.chown(target_file, user="root", group="root") print("3. Updating MariaDB record...") sql = f"UPDATE _1_bucket_1 SET sizeOriginal = {new_size}, sizeActual = {new_size}, signature = '{new_md5}' WHERE _uid = '{FILE_ID}';" cmd = [ "docker", "exec", "-i", "appwrite-mariadb-sss6crntvrcs54qbhvhy5728", "mysql", "-u", "sYoHWwl0DGvEyt3s", "-psn9YkZqoLV06CQxAy2qsKi36S7POVShT", "appwrite", "-e", sql ] res = subprocess.run(cmd, capture_output=True, text=True) if res.returncode != 0: print("MariaDB update error:", res.stderr) else: print("MariaDB record updated successfully.") print("4. Testing HTTP endpoint from Appwrite...") url = f"https://appwrite.almeira.com.co/v1/storage/buckets/media_bucket/files/{FILE_ID}/view?project=69b84df5003bd8957c5c" req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) try: with urllib.request.urlopen(req) as response: status = response.status content_length = response.headers.get("Content-Length") content_type = response.headers.get("Content-Type") print(f"HTTP Status: {status}") print(f"Content-Length: {content_length} bytes (Matches optimized size: {int(content_length) == new_size})") print(f"Content-Type: {content_type}") print("\nSUCCESS! Appwrite is serving the optimized file perfectly!") except Exception as e: print("HTTP Request failed:", e) # Clean up backup if os.path.exists(backup_file): os.remove(backup_file)