@zodic/shared 0.0.344 → 0.0.346
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/api/index.ts +45 -5
- package/package.json +1 -1
package/app/api/index.ts
CHANGED
|
@@ -803,28 +803,68 @@ export const Api = (env: BackendBindings) => ({
|
|
|
803
803
|
}
|
|
804
804
|
};
|
|
805
805
|
|
|
806
|
+
// Helper function to resize an image by calling the Express server
|
|
807
|
+
const resizeImage = async (url: string, label: string): Promise<ArrayBuffer> => {
|
|
808
|
+
const resizeEndpoint = `https://zodiako-image-mounter.onrender.com/resize-image?url=${encodeURIComponent(url)}`; // Replace with your Express server URL
|
|
809
|
+
try {
|
|
810
|
+
const response = await fetch(resizeEndpoint);
|
|
811
|
+
if (!response.ok) {
|
|
812
|
+
throw new Error(`${label} resize failed: ${response.status} ${response.statusText}`);
|
|
813
|
+
}
|
|
814
|
+
const contentType = response.headers.get('Content-Type');
|
|
815
|
+
if (!contentType || !contentType.startsWith('image/')) {
|
|
816
|
+
throw new Error(`${label} resize response is not an image: Content-Type is ${contentType}`);
|
|
817
|
+
}
|
|
818
|
+
return await response.arrayBuffer();
|
|
819
|
+
} catch (err: any) {
|
|
820
|
+
console.error(`Error resizing ${label}:`, err.message);
|
|
821
|
+
throw new Error(`Failed to resize ${label}: ${err.message}`);
|
|
822
|
+
}
|
|
823
|
+
};
|
|
824
|
+
|
|
825
|
+
// Helper function to upload an image to storage and get a public URL
|
|
826
|
+
const uploadImageToStorage = async (imageData: ArrayBuffer, label: string): Promise<string> => {
|
|
827
|
+
const fileName = `${label.toLowerCase().replace(/\s+/g, '-')}-${Date.now()}.jpg`;
|
|
828
|
+
// Replace this with your actual storage service implementation (e.g., Cloudflare R2, AWS S3)
|
|
829
|
+
// Example for Cloudflare R2:
|
|
830
|
+
// const r2Response = await env.R2_BUCKET.put(fileName, imageData, {
|
|
831
|
+
// httpMetadata: { contentType: 'image/jpeg' },
|
|
832
|
+
// });
|
|
833
|
+
// return `https://your-r2-bucket-url/${fileName}`;
|
|
834
|
+
|
|
835
|
+
// For now, we'll throw an error if this isn't implemented
|
|
836
|
+
throw new Error(`Storage service not implemented for ${label}`);
|
|
837
|
+
};
|
|
838
|
+
|
|
806
839
|
try {
|
|
807
840
|
// Validate image URLs
|
|
808
841
|
await validateImageUrl(sourceImageUrl, 'Source image URL');
|
|
809
842
|
await validateImageUrl(targetImageUrl, 'Target image URL');
|
|
810
843
|
|
|
844
|
+
// Resize images by calling the Express server
|
|
845
|
+
const resizedSourceData = await resizeImage(sourceImageUrl, 'Source image');
|
|
846
|
+
const resizedTargetData = await resizeImage(targetImageUrl, 'Target image');
|
|
847
|
+
|
|
848
|
+
// Upload resized images to storage to get public URLs
|
|
849
|
+
const resizedSourceUrl = await uploadImageToStorage(resizedSourceData, 'Source image');
|
|
850
|
+
const resizedTargetUrl = await uploadImageToStorage(resizedTargetData, 'Target image');
|
|
851
|
+
|
|
811
852
|
const body = JSON.stringify({
|
|
812
853
|
model: 'Qubico/image-toolkit',
|
|
813
854
|
type: 'face-swap',
|
|
814
855
|
input: {
|
|
815
|
-
swap_image:
|
|
816
|
-
target_image:
|
|
856
|
+
swap_image: resizedSourceUrl,
|
|
857
|
+
target_image: resizedTargetUrl,
|
|
817
858
|
},
|
|
818
859
|
config: {
|
|
819
860
|
webhook_config: {
|
|
820
|
-
endpoint:
|
|
821
|
-
'https://zodic-backend.lucdelbel.workers.dev/api/webhook/faceswap',
|
|
861
|
+
endpoint: 'https://zodic-backend.lucdelbel.workers.dev/api/webhook/faceswap',
|
|
822
862
|
// secret: '',
|
|
823
863
|
},
|
|
824
864
|
},
|
|
825
865
|
});
|
|
826
866
|
|
|
827
|
-
console.log('Sending FaceSwap request:', {
|
|
867
|
+
console.log('Sending FaceSwap request:', { resizedSourceUrl, resizedTargetUrl });
|
|
828
868
|
|
|
829
869
|
const response = await fetch(endpoint, {
|
|
830
870
|
method: 'POST',
|