Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Pass fs directory from builder (#1400)

Bogdan Chadkinb13b073ffea8
diff --git a/apps/builder/app/shared/asset-client.ts b/apps/builder/app/shared/asset-client.ts
index b100f1d5ba..69d6e3b69c 100644
--- a/apps/builder/app/shared/asset-client.ts
+++ b/apps/builder/app/shared/asset-client.ts
@@ -1,14 +1,16 @@
+import * as path from "path";
import { S3Env, FsEnv } from "@webstudio-is/asset-uploader";
import {
- createFSClient,
+ createFsClient,
createS3Client,
} from "@webstudio-is/asset-uploader/server";
export const createAssetClient = () => {
if (process.env.NODE_ENV === "development") {
const env = FsEnv.parse(process.env);
- return createFSClient({
+ return createFsClient({
maxUploadSize: env.MAX_UPLOAD_SIZE,
+ fileDirectory: path.join(process.cwd(), env.FILE_UPLOAD_PATH),
});
} else {
const env = S3Env.parse(process.env);
diff --git a/packages/asset-uploader/package.json b/packages/asset-uploader/package.json
index e74bd7f744..6adfed5e3d 100644
--- a/packages/asset-uploader/package.json
+++ b/packages/asset-uploader/package.json
@@ -21,7 +21,6 @@
"@webstudio-is/fonts": "workspace:^",
"@webstudio-is/prisma-client": "workspace:^",
"@webstudio-is/trpc-interface": "workspace:^",
- "app-root-path": "^3.0.0",
"immer": "^9.0.12",
"nanoid": "^3.2.0",
"sharp": "^0.30.7",
@@ -29,7 +28,6 @@
},
"devDependencies": {
"@jest/globals": "^29.3.1",
- "@types/app-root-path": "^1.2.4",
"@types/node": "^18.11.18",
"@types/sharp": "^0.30.4",
"@webstudio-is/jest-config": "workspace:^",
diff --git a/packages/asset-uploader/src/clients/fs/delete.ts b/packages/asset-uploader/src/clients/fs/delete.ts
index 1c6958001a..6241eed9a2 100644
--- a/packages/asset-uploader/src/clients/fs/delete.ts
+++ b/packages/asset-uploader/src/clients/fs/delete.ts
@@ -1,14 +1,19 @@
import { unlink } from "fs/promises";
import path from "path";
-import { FILE_DIRECTORY } from "./file-path";
// @todo we need a better place for this to reuse it
const isNodeError = (error: unknown): error is NodeJS.ErrnoException =>
error instanceof Error && "code" in error;
-export const deleteFromFs = async (name: string) => {
+export const deleteFromFs = async ({
+ name,
+ fileDirectory,
+}: {
+ name: string;
+ fileDirectory: string;
+}) => {
try {
- await unlink(path.join(FILE_DIRECTORY, name));
+ await unlink(path.join(fileDirectory, name));
} catch (error) {
// When file was deleted from local file system, we don't need to throw an error.
if (isNodeError(error) && error.code !== "ENOENT") {
diff --git a/packages/asset-uploader/src/clients/fs/file-path.ts b/packages/asset-uploader/src/clients/fs/file-path.ts
deleted file mode 100644
index c0a695177e..0000000000
--- a/packages/asset-uploader/src/clients/fs/file-path.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import path from "path";
-import appRoot from "app-root-path";
-import { FsEnv } from "../../schema";
-const fsEnv = FsEnv.parse(process.env);
-
-export const FILE_DIRECTORY =
- // @todo this will be wrong once we have a local app that is in production and yet saves to local dir
- process.env.NODE_ENV === "production"
- ? path.join(appRoot.path, fsEnv.FILE_UPLOAD_PATH)
- : path.join(appRoot.path, "apps", "builder", fsEnv.FILE_UPLOAD_PATH);
diff --git a/packages/asset-uploader/src/clients/fs/fs.ts b/packages/asset-uploader/src/clients/fs/fs.ts
index 4d0a18cd25..602b49674e 100644
--- a/packages/asset-uploader/src/clients/fs/fs.ts
+++ b/packages/asset-uploader/src/clients/fs/fs.ts
@@ -2,14 +2,20 @@ import type { AssetClient } from "../../client";
import { deleteFromFs } from "./delete";
import { uploadToFs } from "./upload";
-type FSClientOptions = {
+type FsClientOptions = {
+ fileDirectory: string;
maxUploadSize: number;
};
-export const createFSClient = (clientOptions: FSClientOptions): AssetClient => {
- const { maxUploadSize } = clientOptions;
+export const createFsClient = (options: FsClientOptions): AssetClient => {
return {
- uploadFile: (request) => uploadToFs({ request, maxSize: maxUploadSize }),
- deleteFile: deleteFromFs,
+ uploadFile: (request) =>
+ uploadToFs({
+ request,
+ maxSize: options.maxUploadSize,
+ fileDirectory: options.fileDirectory,
+ }),
+ deleteFile: (name) =>
+ deleteFromFs({ name, fileDirectory: options.fileDirectory }),
};
};
diff --git a/packages/asset-uploader/src/clients/fs/upload.ts b/packages/asset-uploader/src/clients/fs/upload.ts
index 1b4271eabc..0db4633d25 100644
--- a/packages/asset-uploader/src/clients/fs/upload.ts
+++ b/packages/asset-uploader/src/clients/fs/upload.ts
@@ -7,7 +7,6 @@ import {
} from "@remix-run/node";
import { Location } from "@webstudio-is/prisma-client";
import { AssetData, getAssetData } from "../../utils/get-asset-data";
-import { FILE_DIRECTORY } from "./file-path";
import { idsFormDataFieldName } from "../../schema";
import { getUniqueFilename } from "../../utils/get-unique-filename";
import { sanitizeS3Key } from "../../utils/sanitize-s3-key";
@@ -24,13 +23,15 @@ const MAX_FILES_PER_REQUEST = 1;
export const uploadToFs = async ({
request,
maxSize,
+ fileDirectory,
}: {
request: Request;
maxSize: number;
+ fileDirectory: string;
}): Promise<AssetData> => {
const uploadHandler = unstableCreateFileUploadHandler({
maxPartSize: maxSize,
- directory: FILE_DIRECTORY,
+ directory: fileDirectory,
file: ({ filename }) => getUniqueFilename(sanitizeS3Key(filename)),
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 28a3e30327..3d4afe75f9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -387,9 +387,6 @@ importers:
'@webstudio-is/trpc-interface':
specifier: workspace:^
version: link:../trpc-interface
- app-root-path:
- specifier: ^3.0.0
- version: 3.0.0
immer:
specifier: ^9.0.12
version: 9.0.15
@@ -406,9 +403,6 @@ importers:
'@jest/globals':
specifier: ^29.3.1
version: 29.3.1
- '@types/app-root-path':
- specifier: ^1.2.4
- version: 1.2.4
'@types/node':
specifier: ^18.11.18
version: 18.11.18
@@ -8229,10 +8223,6 @@ packages:
'@types/estree': 1.0.0
dev: true
- resolution: {integrity: sha512-yhURoXmWN/zfw2MXXcOdUTwe5CWhzRWtb4Rs2+JwGpuhm2hxLCzjASi/aIuyVeJB3Iyks1teaHyOEfNNmltdvQ==}
- dev: true
-
resolution: {integrity: sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==}
dev: false
@@ -9239,11 +9229,6 @@ packages:
resolution: {integrity: sha512-jlpIfsOoNoafl92Sz//64uQHGSyMrD2vYG5d8o2a4qGvyNCvXur7bzIsWtAC/6flI2RYAp3kv8rsfBtaLm7w0g==}
- resolution: {integrity: sha512-qMcx+Gy2UZynHjOHOIXPNvpf+9cjvk3cWrBBK7zg4gH9+clobJRb9NGzcT7mQTcV/6Gm/1WelUtqxVXnNlrwcw==}
- engines: {node: '>= 6.0.0'}
- dev: false
-
resolution: {integrity: sha512-Y9J6ZjXtoYh8RnXVCMOU/ttDmk1aBjunq9vO0ta5x85WDQiQfUF9sIPBITdbiiIVcBo03Hi3jMxigBtsddlXRw==}