Skip to content

Commit

Permalink
feat: update helper image.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
nekaartajaya committed Mar 29, 2023
1 parent 0379b45 commit d17d991
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
2 changes: 0 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const moduleExports = {
firebaseAPIKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
firebaseMessagingSenderId:
process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
// firebaseStorageBucket:
// process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET ?? 'dev.appspot.com',
firebaseAppId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
firebaseMeasurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
},
Expand Down
33 changes: 6 additions & 27 deletions src/helpers/image.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import getConfig from 'next/config';

import path from 'path';
import { Sizes } from 'src/interfaces/assets';

const STORAGE_BASE_URL = 'https://storage.googleapis.com';

export const generateImageSizes = (url: string | Sizes): Sizes => {
const { publicRuntimeConfig } = getConfig();

// Image klo di generate menjadi object
if (typeof url === 'object') {
return {
thumbnail: url.thumbnail,
Expand All @@ -17,27 +11,12 @@ export const generateImageSizes = (url: string | Sizes): Sizes => {
};
}

const external = !url.includes(publicRuntimeConfig.firebaseStorageBucket);
const filename = url.split(/[\\/]/).pop();

if (!filename || external) {
return {
thumbnail: url,
small: url,
medium: url,
large: url,
};
}

const extension = path.parse(url).ext;
const fileId = filename.split('.').slice(0, -1).join('.');
const pathname = url.replace(STORAGE_BASE_URL, '').replace(filename, '');

// Image dari source manapun
return {
thumbnail: `${STORAGE_BASE_URL}${pathname}${fileId}_thumbnail${extension}`,
small: `${STORAGE_BASE_URL}${pathname}${fileId}_small${extension}`,
medium: `${STORAGE_BASE_URL}${pathname}${fileId}_medium${extension}`,
large: `${STORAGE_BASE_URL}${pathname}${fileId}${extension}`,
thumbnail: url,
small: url,
medium: url,
large: url,
};
};

Expand Down

0 comments on commit d17d991

Please sign in to comment.