Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use CDN key from grindor completed API #322

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sample-upload.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions sample-upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// ! DO NOT REMOVE
// This file is used to get CDN base path
2 changes: 1 addition & 1 deletion src/helper/serve.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export async function startServer({ domain, host, isSSR, port }) {
'fdk-cli-dev-files',
User.current_user._id,
)
).start.cdn.url;
).complete.cdn.url;
} else {
jetfireUrl.searchParams.set('__csr', 'true');
}
Expand Down
66 changes: 35 additions & 31 deletions src/lib/Theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export default class Theme {
public static async getThemeBundle(stats: MultiStats) {
const fileList = stats.stats[0].toJson().assets.map(({ name }) => name);
const outputFileName= fileList.find(file => file.startsWith('themeBundle') && file.endsWith('.js'));

const buildPath = path.join(process.cwd(), Theme.BUILD_FOLDER);
const outputFilePath = path.resolve(buildPath, outputFileName);

Expand Down Expand Up @@ -1680,20 +1679,22 @@ export default class Theme {
};

private static getImageCdnBaseUrl = async () => {
let imageCdnUrl = '';
try {
let startData = {
file_name: 'test.jpg',
content_type: 'image/jpeg',
size: '1',
};
let startAssetData = (
await UploadService.startUpload(
karanrainafynd marked this conversation as resolved.
Show resolved Hide resolved
startData,
'application-theme-images',
)
).data;
return (imageCdnUrl = path.dirname(startAssetData.cdn.url));
const dummyFile = path.join(
__dirname,
'..',
'..',
'sample-upload.jpeg'
);

const response = await UploadService.uploadFile(
dummyFile,
'application-theme-images',
null,
'image/jpeg'
);

return path.dirname(response.complete.cdn.url);
karanrainafynd marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
Logger.error(err);
throw new CommandError(
Expand All @@ -1704,20 +1705,23 @@ export default class Theme {
};

private static getAssetCdnBaseUrl = async () => {
let assetCdnUrl = '';
try {
const startData = {
file_name: 'test.ttf',
content_type: 'font/ttf',
size: '10',
};
const startAssetData = (
await UploadService.startUpload(
startData,
'application-theme-assets',
)
).data;
return (assetCdnUrl = path.dirname(startAssetData.cdn.url));
const dummyFile = path.join(
__dirname,
'..',
'..',
'sample-upload.js'
);

const response = await UploadService.uploadFile(
dummyFile,
'application-theme-assets',
null,
'application/javascript'
);

return path.dirname(response.complete.cdn.url);

} catch (err) {
throw new CommandError(
`Failed in getting assets CDN base url`,
Expand Down Expand Up @@ -1884,7 +1888,7 @@ export default class Theme {
path.join(process.cwd(), Theme.BUILD_FOLDER, commonJS),
'application-theme-assets',
);
const commonJsUrl = commonJsUrlRes.start.cdn.url;
const commonJsUrl = commonJsUrlRes.complete.cdn.url;

Logger.info('Uploading umdJS');
const umdMinAssets = glob.sync(
Expand Down Expand Up @@ -1921,9 +1925,9 @@ export default class Theme {
});
const cssUrls = await Promise.all(cssPromisesArr);
return [
cssUrls.map((res) => res.start.cdn.url),
cssUrls.map((res) => res.complete.cdn.url),
commonJsUrl,
umdJsUrls.map((res) => res.start.cdn.url),
umdJsUrls.map((res) => res.complete.cdn.url),
];
} catch (err) {
throw new CommandError(
Expand Down Expand Up @@ -2528,7 +2532,7 @@ export default class Theme {
zipFilePath,
'application-theme-src',
);
return res.start.cdn.url;
return res.complete.cdn.url;
} catch (err) {
throw new CommandError(
err.message || `Failed to upload src folder`,
Expand Down
24 changes: 4 additions & 20 deletions src/lib/api/services/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,8 @@ import mime from 'mime';
import Spinner from '../../../helper/spinner';

export default {
startUpload: async (data, namespace) => {
try {
const axiosOption = Object.assign(
{},
{
data: data,
},
getCommonHeaderOptions(),
);
const res = await ApiClient.post(
URLS.START_UPLOAD_FILE(namespace),
axiosOption,
);
return res;
} catch (error) {
throw error;
}
},
uploadFile: async (filepath, namespace, file_name = null) => {

uploadFile: async (filepath, namespace, file_name = null, mimeType = null) => {
karanrainafynd marked this conversation as resolved.
Show resolved Hide resolved
karanrainafynd marked this conversation as resolved.
Show resolved Hide resolved
let spinner = new Spinner();
let textMessage;
try {
Expand All @@ -35,7 +18,8 @@ export default {
filepath,
)} [${Math.round(stats.size / 1024)} KB]`;
spinner.start(textMessage);
let contentType = mime.getType(path.extname(filepath));
let contentType = mimeType || mime.getType(path.extname(filepath));

if (contentType === 'image/jpg') {
contentType = 'image/jpeg';
}
Expand Down
Loading