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

Add more image info to DB #148

Merged
merged 16 commits into from
Jul 17, 2022
Prev Previous commit
Next Next commit
clean up fetching info
  • Loading branch information
SaraVieira committed Jul 16, 2022
commit b406e3d27bf909cc6faf4ba4fb665cfeb5749ba7
6 changes: 2 additions & 4 deletions apps/designer/app/designer/features/sidebar-left/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { panels } from "./panels";
import { z } from "zod";
import { NodeOnDiskFile } from "@remix-run/node";

export type TabName = keyof typeof panels | "none";

const SingleImageInUpload = z.object({
name: z.string(),
type: z.string(),
});
const SingleImageInUpload = z.instanceof(NodeOnDiskFile);

export const ImagesUpload = z.array(SingleImageInUpload);

Expand Down
8 changes: 5 additions & 3 deletions apps/designer/app/routes/designer/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export const action: ActionFunction = async ({ request, params }) => {

const imagesInfo = ImagesUpload.parse(formData.getAll("image"));

const allInfo = imagesInfo.map(async (image) => {
const allInfo = imagesInfo.map(async (image, i) => {
const data = {
name: image.name,
path: path.join("/", folderInPublic, image.name),
size: image.size,
};
const absolutePath = path.join(directory, image.name);
const arrayBuffer = await image.arrayBuffer();
const projectId = params.id as string;
const newAsset = await db.assets.create(projectId, data, absolutePath);
const newAsset = await db.assets.create(projectId, data, arrayBuffer);

return newAsset;
});
Expand All @@ -71,6 +72,7 @@ export const action: ActionFunction = async ({ request, params }) => {
ok: true,
};
} catch (error) {
console.log(error);
if (error instanceof Error) {
return {
errors: error.message,
Expand Down
21 changes: 3 additions & 18 deletions apps/designer/app/shared/db/assets.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { type Project } from "@webstudio-is/react-sdk";
import { prisma } from "./prisma.server";
import sharp from "sharp";
import fsPromises from "fs/promises";
import { fetch } from "@remix-run/node";

export const loadByProject = async (projectId?: Project["id"]) => {
if (typeof projectId !== "string") {
Expand All @@ -18,27 +16,14 @@ export const loadByProject = async (projectId?: Project["id"]) => {

export const create = async (
projectId: Project["id"],
values: { name: string; path: string },
absolutePath?: string
values: { name: string; path: string; size: number },
arrayBuffer: Uint8Array
) => {
let size = 0;
const image = await sharp(absolutePath);
const image = sharp(arrayBuffer);
const metadata = await image.metadata();

// if there is no absolute path then it means it's a remote image and we can use fetch
if (absolutePath) {
size = await (await fsPromises.stat(absolutePath)).size;
} else {
const arrayBuffer = await fetch(values.path).then((rsp) =>
rsp.arrayBuffer()
);
size = arrayBuffer.byteLength;
}

const newAsset = await prisma.asset.create({
data: {
...values,
size,
format: metadata.format,
...(metadata.width ? { width: Math.round(metadata.width) } : {}),
SaraVieira marked this conversation as resolved.
Show resolved Hide resolved
...(metadata.height ? { height: Math.round(metadata.height) } : {}),
Expand Down