Refactor into usage

This commit is contained in:
Clément Pasteau
2023-02-14 15:37:14 +01:00
parent 3f36054d54
commit ba4a37813e
5 changed files with 45 additions and 44 deletions

View File

@@ -26,7 +26,10 @@ import {
ExplanationHeader,
OnlineGameLink,
} from '../GenericExporters/OnlineWebExport';
import { hasValidSubscriptionPlan } from '../../Utils/GDevelopServices/Usage';
import {
hasValidSubscriptionPlan,
onlineWebExportSizeOptions,
} from '../../Utils/GDevelopServices/Usage';
const gd: libGDevelop = global.gd;
type ExportState = null;
@@ -49,9 +52,6 @@ type ResourcesDownloadOutput = {|
type CompressionOutput = Blob;
const FREE_SIZE_LIMIT_IN_MB = 50;
const SUBSCRIBED_SIZE_LIMIT_IN_MB = 250;
export const browserOnlineWebExportPipeline: ExportPipeline<
ExportState,
PreparedExporter,
@@ -162,19 +162,10 @@ export const browserOnlineWebExportPipeline: ExportPipeline<
textFiles,
basePath: '/export/',
onProgress: context.updateStepProgress,
sizeOptions: {
// Higher limit for users with a subscription.
limit:
(hasValidSubscription
? SUBSCRIBED_SIZE_LIMIT_IN_MB
: FREE_SIZE_LIMIT_IN_MB) *
1000 *
1000,
getMessage: fileSizeInMb =>
hasValidSubscription
? `Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB.`
: `Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${FREE_SIZE_LIMIT_IN_MB} MB. You can subscribe to GDevelop to increase the limit to ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB.`,
},
// Higher limit for users with a subscription.
sizeOptions: hasValidSubscription
? onlineWebExportSizeOptions.subscribed
: onlineWebExportSizeOptions.guest,
});
},

View File

@@ -22,16 +22,16 @@ import {
OnlineGameLink,
} from '../GenericExporters/OnlineWebExport';
import { downloadUrlsToLocalFiles } from '../../Utils/LocalFileDownloader';
import { hasValidSubscriptionPlan } from '../../Utils/GDevelopServices/Usage';
import {
hasValidSubscriptionPlan,
onlineWebExportSizeOptions,
} from '../../Utils/GDevelopServices/Usage';
const path = optionalRequire('path');
const os = optionalRequire('os');
const gd: libGDevelop = global.gd;
type ExportState = null;
const FREE_SIZE_LIMIT_IN_MB = 50;
const SUBSCRIBED_SIZE_LIMIT_IN_MB = 250;
type PreparedExporter = {|
exporter: gdjsExporter,
localFileSystem: LocalFileSystem,
@@ -165,19 +165,10 @@ export const localOnlineWebExportPipeline: ExportPipeline<
return archiveLocalFolder({
path: temporaryOutputDir,
outputFilename: path.join(archiveOutputDir, 'game-archive.zip'),
sizeOptions: {
// Higher limit for users with a subscription.
limit:
(hasValidSubscription
? SUBSCRIBED_SIZE_LIMIT_IN_MB
: FREE_SIZE_LIMIT_IN_MB) *
1000 *
1000,
getMessage: fileSizeInMb =>
hasValidSubscription
? `Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB.`
: `Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${FREE_SIZE_LIMIT_IN_MB} MB. You can subscribe to GDevelop to increase the limit to ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB.`,
},
// Higher limit for users with a subscription.
sizeOptions: hasValidSubscription
? onlineWebExportSizeOptions.subscribed
: onlineWebExportSizeOptions.guest,
});
},

View File

@@ -3,6 +3,7 @@ import { initializeZipJs } from './Zip.js';
import { downloadUrlsToBlobs, type ItemResult } from './BlobDownloader';
import path from 'path';
import { shortenString } from './StringHelpers.js';
import { type ExportSizeOptions } from './GDevelopServices/Usage';
export type BlobFileDescriptor = {|
filePath: string,
@@ -101,10 +102,7 @@ export const archiveFiles = async ({
blobFiles: Array<BlobFileDescriptor>,
basePath: string,
onProgress: (count: number, total: number) => void,
sizeOptions?: {|
limit: number,
getMessage: (fileSizeInMb: number) => string,
|},
sizeOptions?: ExportSizeOptions,
|}): Promise<Blob> => {
const zipJs: ZipJs = await initializeZipJs();
@@ -162,7 +160,7 @@ export const archiveFiles = async ({
fileSize / (1000 * 1000)
);
reject(
new Error(sizeOptions.getMessage(roundFileSizeInMb))
new Error(sizeOptions.getErrorMessage(roundFileSizeInMb))
);
}
resolve(blob);

View File

@@ -190,6 +190,29 @@ export const businessPlan: PlanDetails = {
descriptionBullets: [],
};
const SUBSCRIBED_SIZE_LIMIT_IN_MB = 250;
const GUEST_SIZE_LIMIT_IN_MB = 50;
export type ExportSizeOptions = {|
limit: number,
getErrorMessage: (fileSizeInMb: number) => string,
|};
export const onlineWebExportSizeOptions: {
[key: 'guest' | 'subscribed']: ExportSizeOptions,
} = {
guest: {
limit: SUBSCRIBED_SIZE_LIMIT_IN_MB * 1000 * 1000,
getErrorMessage: (fileSizeInMb: number) =>
`Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB`,
},
subscribed: {
limit: GUEST_SIZE_LIMIT_IN_MB * 1000 * 1000,
getErrorMessage: (fileSizeInMb: number) =>
`Archive is of size ${fileSizeInMb} MB, which is above the limit allowed of ${GUEST_SIZE_LIMIT_IN_MB} MB. You can subscribe to GDevelop to increase the limit to ${SUBSCRIBED_SIZE_LIMIT_IN_MB} MB.`,
},
};
export const getUserUsages = (
getAuthorizationHeader: () => Promise<string>,
userId: string

View File

@@ -2,6 +2,7 @@
import optionalRequire from './OptionalRequire';
import optionalLazyRequire from '../Utils/OptionalLazyRequire';
import { type ExportSizeOptions } from './GDevelopServices/Usage';
const fs = optionalRequire('fs');
const lazyRequireArchiver = optionalLazyRequire('archiver');
@@ -16,10 +17,7 @@ export const archiveLocalFolder = ({
}: {|
path: string,
outputFilename: string,
sizeOptions?: {|
limit: number,
getMessage: (fileSizeInMb: number) => string,
|},
sizeOptions?: ExportSizeOptions,
|}): Promise<string> => {
const archiver = lazyRequireArchiver();
return new Promise((resolve, reject) => {
@@ -37,7 +35,7 @@ export const archiveLocalFolder = ({
);
if (sizeOptions && fileSize > sizeOptions.limit) {
const roundFileSizeInMb = Math.round(fileSize / (1000 * 1000));
reject(new Error(sizeOptions.getMessage(roundFileSizeInMb)));
reject(new Error(sizeOptions.getErrorMessage(roundFileSizeInMb)));
}
resolve(outputFilename);
});