from BunnyCDN.Storage import Storage
import config, os

db, cursor = config.gen_connection()
obj_storage = Storage('345697f9-d9aa-4a6b-a5ec8bffc16d-ceaf-453e', 'storysave')

cursor.execute("SELECT id, media_id, media_url FROM media WHERE file_size = 0;")
results = cursor.fetchall()
print(f"Found {len(results)} files to process.")

cacheDir = 'cache'

for result in results:
    itemID = result[0]

    mediaURL = result[2]
    serverPath = mediaURL.replace("https://storysave.b-cdn.net/", '').replace('//', '/').replace('\\', '/')
    localFilePath = os.path.join(cacheDir, os.path.basename(serverPath))

    if not os.path.exists(localFilePath):
        continue    

    file_size = os.path.getsize(localFilePath)

    cursor.execute("UPDATE media SET file_size = %s WHERE id = %s;", (file_size, itemID))
    db.commit()
    
    print(f"Processed ID {itemID}: updated with file size {file_size}.")

cursor.close()
db.close()