Skip to content

Commit

Permalink
add update video
Browse files Browse the repository at this point in the history
  • Loading branch information
mayaif committed Aug 14, 2024
1 parent 7f10c5c commit 0675e09
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions lib/appwrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,15 @@ export const getFilePreview = async (fileId, type) => {

export const uploadFile = async (file, type) => {
if (!file) return;
const fileName = file.fileName || file.uri.split("/").pop();

const asset = {
name: file.fileName,
name: fileName,
type: file.mimeType,
size: file.fileSize,
uri: file.uri,
};

try {
const uploadedFile = await storage.createFile(
storageId,
Expand Down Expand Up @@ -248,6 +251,43 @@ export const createVideo = async (form) => {
throw new Error(error);
}
};
export const updateVideo = async (videoId, updatedData) => {
try {
let thumbnailUrl = updatedData.thumbnail;
let videoUrl = updatedData.video;

if (thumbnailUrl && typeof thumbnailUrl === "object" && thumbnailUrl.uri) {
thumbnailUrl = await uploadFile(thumbnailUrl, "image");
}

if (videoUrl && typeof videoUrl === "object" && videoUrl.uri) {
videoUrl = await uploadFile(videoUrl, "video");
}

const updatePayload = {
...(updatedData.title && { title: updatedData.title }),
...(updatedData.prompt && { prompt: updatedData.prompt }),
...(thumbnailUrl && { thumbnail: thumbnailUrl }),
...(videoUrl && { video: videoUrl }),
};

if (Object.keys(updatePayload).length > 0) {
const updatedVideo = await databases.updateDocument(
databaseId,
videoCollectionId,
videoId,
updatePayload
);
return updatedVideo;
} else {
throw new Error("No fields provided for update");
}
} catch (error) {
throw new Error(
error.message || "An error occurred while updating the video."
);
}
};

// Followers
export const addFollower = async (followerData) => {
Expand Down Expand Up @@ -486,13 +526,9 @@ export const getSavedVideos = async (userId) => {
});

const videoDetails = await Promise.all(videoDetailsPromises);
// Filter out null results (failed fetches)
const validVideoDetails = videoDetails.filter((video) => video !== null);
// Extract creator IDs
const creatorIds = validVideoDetails.map((video) => video.creatorId);
// Remove duplicates from creatorIds
const uniqueCreatorIds = [...new Set(creatorIds)];
// Fetch creator details for each unique creatorId
const creatorDetailsPromises = uniqueCreatorIds.map(async (creatorId) => {
try {
return await databases.getDocument(
Expand Down

0 comments on commit 0675e09

Please sign in to comment.