Skip to content

Commit

Permalink
changed rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoann committed Oct 28, 2023
1 parent 8f150f5 commit 1597793
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
{
"key": "Access-Control-Allow-Origin",
"value": "https://recipebook2-a9e9a.web.app"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Origin, X-Requested-With, Content-Type, Accept"
}
]
}
Expand Down
57 changes: 42 additions & 15 deletions public/scripts/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,37 +313,64 @@ export function displayRecipes(database, type, profile = database.user) {
createMenu(database, recipeDiv, recipeName);
} else if (type === "profile") {
Promise.all(forkPromises).then(() => {
createForkBtn(database, recipeDiv, forkRecipe, profile);
createForkBtn(database, recipeDiv, forkRecipe, recipeImg);
});
}
}
});
}

function createForkBtn(database, recipeDiv, recipe, profile) {
function createForkBtn(database, recipeDiv, recipe, recipeImg) {
const forkBtn = document.createElement("div");
forkBtn.classList.add("fork-btn");
forkBtn.innerHTML = "fork";
recipeDiv.appendChild(forkBtn);

forkBtn.addEventListener("click", () => {
const user = firebase.auth().currentUser;
user.getIdToken().then((token) => {
database.getRecipeImage(recipe.name, profile).then((url) => {
fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => response.blob())
.then((blob) => {
const image = new File([blob], "image.png", { type: "image/png" });
database.addRecipe(recipe, image);
});
fetch(recipeImg.src).then((response) => {
response.blob().then((blob) => {
const fileObject = new File([blob], "image.jpg", {
type: "image/jpeg",
});
database.addRecipe(recipe, fileObject);
});
});
// match /users/{userId}/{allPaths=**} {
// allow read, write: if request.auth != null && request.auth.uid == userId && request.auth.uid == request.path[1];
// }

// const sourceRef = firebase
// .storage()
// .ref(`${profile}/images/${recipe.name}`);
// sourceRef.getDownloadURL().then((sourceUrl) => {
// const destRef = firebase
// .storage()
// .ref(`${database.user}/images/${recipe.name}`);
// destRef.getDownloadURL().then((destUrl) => {
// copyImage(sourceUrl, destUrl);
// });
// });
// });
});
}
// async function copyImage(sourceUrl, destinationPath) {
// try {
// const response = await fetch("/.workflows/workflow-name", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// sourceUrl: sourceUrl,
// destinationPath: destinationPath,
// }),
// });
// const result = await response.text();
// console.log(result);
// } catch (error) {
// console.error("Error copying image:", error);
// }
// }

async function handleShoppingToggle(database, recipeName) {
database.updateShoppingList(recipeName);
Expand Down
8 changes: 3 additions & 5 deletions storage.rules
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ rules_version = '2';
// /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
}
match /users/{userId}/{allPaths=**} {
allow read, write: if request.auth != null && request.auth.uid == userId && request.auth.uid == request.path[1];
match /{allPaths=**} {
allow read: if request.auth != null || request.auth == null && request.headers.origin == "https://recipebook2-a9e9a.web.app";
allow write;
}
}
}
25 changes: 25 additions & 0 deletions workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
main:
steps:
- copyImage:
try:
call: http.request
args:
method: GET
url: "${params.sourceUrl}" # Get the image from the source URL
next:
- uploadImage

- uploadImage:
try:
call: http.request
args:
method: PUT
url: "https://firebasestorage.googleapis.com/v0/b/recipebook2-a9e9a.appspot.com/o/${params.destinationPath}"
headers:
Content-Type: "image/jpeg" # Adjust the content type based on your image format
body: "${copyImage.body}" # Use the body of the fetched image as the body for the PUT request
next:
- done

- done:
return: "File copied successfully!"

0 comments on commit 1597793

Please sign in to comment.