Skip to content

Commit

Permalink
Store image data and remove url from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyavila committed Dec 6, 2023
1 parent 31e35f0 commit cbb0f4b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
16 changes: 10 additions & 6 deletions Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import requests
from flask_cors import CORS, cross_origin

from mongoengine import connect, Document, StringField, DoesNotExist
from mongoengine import connect, Document, StringField, BinaryField, DoesNotExist
from werkzeug.security import generate_password_hash, check_password_hash
import secrets # For generating a session key

Expand All @@ -20,6 +20,8 @@
from db_access import Image
from db_access import check_user, create_user, db_connect, get_image, create_image

import base64


app = Flask(__name__)
cors = CORS(app)
Expand Down Expand Up @@ -91,11 +93,13 @@ def store_image():
if field not in data:
return jsonify({"error": f"Missing field: {field}"}), 400

image_res = requests.get(data["url"])

# Prepare the data for creating an image
image_data = {
"creator": current_user, # Assuming the creator is the logged-in user
"prompt": data["prompt"],
"url": data["url"]
"data": image_res.content
}

# Call the create_image function from db_access
Expand All @@ -118,15 +122,15 @@ def fetch_portfolio():
user = get_jwt_identity()

# Fetch all images created by this user
user_images = Image.objects(creator=user)
portfolio_images = User.objects.get(pk=user).portfolio

# Format the response with the list of images
portfolio = [{
"image_id": str(image.id),
"prompt": image.prompt,
"url": image.url,
"creator": user
} for image in user_images]
"creator": user,
"data": base64.b64encode(image.data).decode("ascii")
} for image in portfolio_images]

return jsonify(portfolio), 200

Expand Down
10 changes: 6 additions & 4 deletions Backend/db_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
connect,
Document,
StringField,
BinaryField,
IntField,
ListField,
ReferenceField,
Expand Down Expand Up @@ -47,8 +48,8 @@ class User(Document):

class Image(Document):
creator = ReferenceField(User, required=True)
url = StringField(required=True)
prompt = StringField(required=True)
data = BinaryField(required=True)

meta = {"collection": "images"}

Expand All @@ -60,7 +61,6 @@ def create_user(data):
encrypted_password=data["password"],
email=data["email"],
portfolio=[] # Initialize an empty portfolio

)
try:
user.save()
Expand Down Expand Up @@ -95,12 +95,14 @@ def create_image(data):
image = Image(
creator=data["creator"],
prompt=data["prompt"],
url=data["url"]
data=data["data"]
)
image.save()

creator = User.objects.get(pk=data["creator"])

# Optionally, you can also append this image to the user's portfolio here
#creator.update(push__portfolio=image)
creator.update(push__portfolio=image)

return Response("Image created successfully!", 201)
except DoesNotExist:
Expand Down
10 changes: 4 additions & 6 deletions Frontend/src/pages/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default function Portfolio() {
"Authorization": `Bearer ${Cookie.get("token")}`
}
}).then(response => {
console.log(response.data);
setPortfolio(response.data);
}).catch((error) => console.error("Error: ", error));
}
Expand All @@ -28,11 +27,10 @@ export default function Portfolio() {
<h1 className="text-2xl mb-4">Portfolio</h1>
<div className="grid grid-cols-3 gap-4">
{portfolio && portfolio.map((image) =>
<Image
loader={() => image.url}
src={image.url}
width={500}
height={500}
<Image src={"data:image/png;base64, " + image.data}
loader={() => image.url}
height={500}
width={500}
/>)}
</div>
</div>
Expand Down

0 comments on commit cbb0f4b

Please sign in to comment.