1- import fs from "fs"
2- import { list , put } from "@vercel/blob"
3-
4- import {
5- absPathFromPublic ,
6- getNonIgnoredFiles ,
7- relativePathFromPublic ,
8- } from "@/lib/fileHelper"
9- import { getRedisClient } from "@/lib/utils/redis"
10-
1+ import { put } from "@vercel/blob"
112export async function uploadImageToVercel (
123 buffer : Buffer ,
134 pathFromPublic : string
@@ -34,85 +25,3 @@ function generateImageUrl(pathFromPublic: string) {
3425 }
3526 return `https://pcpfoetqkuq7jmso.public.blob.vercel-storage.com/${ pathFromPublic } `
3627}
37-
38- export async function uploadPublicImagesToVercel ( ) {
39- const urls = [ ]
40- const imageFiles = getImagesFromPublic ( )
41- for ( const pathFromPublic of imageFiles ) {
42- let url = await uploadImageToVercelIfNecessary ( pathFromPublic )
43- urls . push ( url )
44- }
45- return urls
46- }
47-
48- function getImagesFromPublic ( ) {
49- const allFiles = getNonIgnoredFiles ( absPathFromPublic ( "" ) )
50- const imageFiles = [ ]
51- for ( const file of allFiles ) {
52- if (
53- file . endsWith ( ".png" ) ||
54- file . endsWith ( ".jpg" ) ||
55- file . endsWith ( ".jpeg" )
56- ) {
57- imageFiles . push ( file )
58- }
59- }
60- return imageFiles . map ( relativePathFromPublic )
61- }
62-
63- export async function uploadImageToVercelIfNecessary ( pathFromPublic : string ) {
64- let url = await vercelImageExists ( pathFromPublic )
65- if ( url ) {
66- console . log ( `Image already uploaded: ${ pathFromPublic } ` )
67- return
68- }
69- const buffer = fs . readFileSync ( absPathFromPublic ( pathFromPublic ) )
70- url = await uploadImageToVercel ( buffer , pathFromPublic )
71- console . log ( `Uploaded: ${ url } ` )
72- return url
73- }
74-
75- export async function downloadAllBlobImages ( ) {
76- const listBlobResult = await list ( )
77- const blobs = listBlobResult . blobs
78- // Loop through array of blobs
79- for ( const blob of blobs ) {
80- const url = blob . url
81- // Download image from url
82- const response = await fetch ( url )
83- const buffer = await response . text ( )
84- let pathname = relativePathFromPublic ( blob . pathname )
85- const absPath = absPathFromPublic ( pathname )
86- if ( fs . existsSync ( absPath ) ) {
87- console . log ( `Image already exists: ${ absPath } ` )
88- continue
89- }
90- fs . writeFileSync ( absPath , buffer )
91- }
92- }
93-
94- export async function listImagesOnVercel ( ) {
95- const cacheKey = "vercelImages"
96- const cachedImages = await getRedisClient ( ) . get ( cacheKey )
97- if ( cachedImages ) {
98- return JSON . parse ( cachedImages )
99- }
100- const listBlobResult = await list ( )
101- const blobs = listBlobResult . blobs
102- const urls = [ ]
103- for ( const blob of blobs ) {
104- urls . push ( blob . url )
105- }
106- await getRedisClient ( ) . set ( cacheKey , JSON . stringify ( urls ) )
107- return urls
108- }
109-
110- export async function getVercelImageUrlFromPath ( pathFromPublic : string ) {
111- const vercelImages = await listImagesOnVercel ( )
112- for ( const url of vercelImages ) {
113- if ( url . includes ( pathFromPublic ) ) {
114- return url
115- }
116- }
117- return null
118- }
0 commit comments