File size: 3,508 Bytes
7405474
 
 
756b14b
 
 
7405474
 
 
 
 
756b14b
2d94c1e
 
 
756b14b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7405474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
756b14b
7405474
756b14b
2d94c1e
 
 
 
 
7405474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
756b14b
 
 
 
 
 
 
 
2d94c1e
7405474
 
2d94c1e
 
 
 
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
import streamlit as st
import os
from PIL import Image
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2.credentials import Credentials
from chainguard.blockchain_logger import BlockchainLogger

# Initialize Blockchain Logger
blockchain_logger = BlockchainLogger()

# Directory for local storage of uploaded files
UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

# Load Google Drive API credentials
def get_drive_service():
    creds = Credentials.from_authorized_user_file("credentials.json", scopes=["https://www.googleapis.com/auth/drive.file"])
    return build('drive', 'v3', credentials=creds)

drive_service = get_drive_service()

def upload_to_google_drive(file_path, folder_id):
    """Uploads a file to Google Drive in the specified folder."""
    file_metadata = {
        'name': os.path.basename(file_path),
        'parents': [folder_id]
    }
    media = MediaFileUpload(file_path, resumable=True)
    file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    return file.get('id')

def log_metadata(file_name, tags, album):
    """Log photo metadata to Chagu blockchain."""
    metadata = {
        "file_name": file_name,
        "tags": tags,
        "album": album
    }
    block_details = blockchain_logger.log_data(metadata)
    return block_details

# Streamlit App Layout
st.title("Memora: Photo & Video Uploader")
st.subheader("Securely upload and organize your memories")

# File Upload
uploaded_files = st.file_uploader(
    "Upload your photos or videos", accept_multiple_files=True, type=['jpg', 'jpeg', 'png', 'mp4', 'avi']
)

if uploaded_files:
    folder_id = st.text_input("Enter your Google Drive Folder ID", "")
    for uploaded_file in uploaded_files:
        # Save file locally
        file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getbuffer())
        st.success(f"File {uploaded_file.name} saved locally in {UPLOAD_DIR}.")

        # Display uploaded file
        st.write(f"File Name: {uploaded_file.name}")
        if uploaded_file.type.startswith('image'):
            image = Image.open(uploaded_file)
            st.image(image, caption=uploaded_file.name, use_column_width=True)

        # Metadata Input
        album = st.text_input(f"Album for {uploaded_file.name}", value="Default Album")
        tags = st.text_input(f"Tags for {uploaded_file.name} (comma-separated)", value="")

        # Log Metadata
        if st.button(f"Log Metadata for {uploaded_file.name}"):
            metadata = log_metadata(uploaded_file.name, tags.split(','), album)
            st.write(f"Metadata logged successfully! Block Details: {metadata}")

        # Upload to Google Drive
        if folder_id and st.button(f"Upload {uploaded_file.name} to Google Drive"):
            try:
                drive_file_id = upload_to_google_drive(file_path, folder_id)
                st.success(f"File {uploaded_file.name} uploaded to Google Drive with File ID: {drive_file_id}")
            except Exception as e:
                st.error(f"Failed to upload {uploaded_file.name} to Google Drive: {str(e)}")

# Blockchain Integrity Validation
if st.button("Validate Blockchain Integrity"):
    is_valid = blockchain_logger.is_blockchain_valid()
    if is_valid:
        st.success("Blockchain Integrity: Valid βœ…")
    else:
        st.error("Blockchain Integrity: Invalid ❌")