Skip to content
Closed
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
11 changes: 8 additions & 3 deletions apps/builder/app/builder/features/pages/image-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ImageIcon,
} from "@webstudio-is/icons";
import type { ImageAsset } from "@webstudio-is/sdk";
import { formatAssetName } from "~/builder/shared/assets/asset-utils";
import { getFormattedAspectRatio } from "~/builder/shared/image-manager/utils";

type ImageInfoProps = {
Expand Down Expand Up @@ -42,10 +43,14 @@ export const ImageInfo = ({ asset, onDelete }: ImageInfoProps) => {
gap={2}
align={"center"}
>
<Grid flow={"column"} gap={1} align={"center"}>
<Grid
gap={1}
align="center"
css={{ gridTemplateColumns: "max-content 1fr" }}
>
<ImageIcon />
<Text truncate variant={"labelsTitleCase"}>
{asset.name}
<Text truncate variant={"labelsSentenceCase"}>
{formatAssetName(asset)}
</Text>
</Grid>
<Grid columns={2} gap={1} align={"center"}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ $project.set({
projectId: "projectId",
id: "imageId",
name: "very-very-very-long-long-image-name.jpg",
filename: null,
description: null,
},
latestBuildVirtual: null,
domainsVirtual: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { $assets } from "~/shared/nano-states";
import { ImageManager } from "~/builder/shared/image-manager";
import { type ControlProps } from "../shared";
import { acceptToMimeCategories } from "@webstudio-is/asset-uploader";
import { formatAssetName } from "~/builder/shared/assets/asset-utils";

// tests whether we can use ImageManager for the given "accept" value
const isImageAccept = (accept?: string) => {
Expand Down Expand Up @@ -52,7 +53,7 @@ export const SelectAsset = ({ prop, onChange, accept }: Props) => {
}
>
<Button color="neutral" css={{ flex: 1 }}>
{asset?.name ?? "Choose source"}
{asset ? formatAssetName(asset) : "Choose source"}
</Button>
</FloatingPanel>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ const imageAsset = (name = "cat", format = "jpg"): Asset => ({
format: format,
size: 100000,
createdAt: new Date().toISOString(),
description: null,
meta: { width: 128, height: 180 },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getRepeatedStyleItem,
setRepeatedStyleItem,
} from "../../shared/repeated-style";
import { formatAssetName } from "~/builder/shared/assets/asset-utils";

const isValidURL = (value: string) => {
try {
Expand Down Expand Up @@ -115,7 +116,7 @@ export const ImageControl = ({
color="neutral"
css={{ maxWidth: "100%", justifySelf: "right" }}
>
{asset?.name ?? "Choose image..."}
{asset ? formatAssetName(asset) : "Choose image..."}
</Button>
</FloatingPanel>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import brokenImage from "~/shared/images/broken-image-placeholder.svg";
import { humanizeString } from "~/shared/string-utils";
import { useComputedStyles } from "../../shared/model";
import { getComputedRepeatedItem } from "../../shared/repeated-style";
import { formatAssetName } from "~/builder/shared/assets/asset-utils";

export const repeatedProperties = [
"background-image",
Expand Down Expand Up @@ -89,7 +90,7 @@ export const getBackgroundLabel = (
) {
const asset = assets.get(backgroundImageStyle.value.value);
if (asset) {
return asset.name;
return formatAssetName(asset);
}
}

Expand Down
25 changes: 25 additions & 0 deletions apps/builder/app/builder/shared/assets/asset-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test } from "vitest";
import { parseAssetName } from "./asset-utils";

test("parse asset name", () => {
expect(parseAssetName("hello_hash.ext")).toEqual({
basename: "hello",
hash: "hash",
ext: "ext",
});
expect(parseAssetName("hello.ext")).toEqual({
basename: "hello",
hash: "",
ext: "ext",
});
expect(parseAssetName("hello_hash1.ext_hash2")).toEqual({
basename: "hello",
hash: "hash1",
ext: "ext_hash2",
});
expect(parseAssetName("hello_hash1_hash2")).toEqual({
basename: "hello_hash1",
hash: "hash2",
ext: "",
});
});
28 changes: 28 additions & 0 deletions apps/builder/app/builder/shared/assets/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,31 @@ export const uploadingFileDataToAsset = (

return asset;
};

type ParsedAssetName = {
basename: string;
hash: string;
ext: string;
};

export const parseAssetName = (name: string): ParsedAssetName => {
let hash = "";
let ext = "";
const lastDotAt = name.lastIndexOf(".");
if (lastDotAt > -1) {
ext = name.slice(lastDotAt + 1);
name = name.slice(0, lastDotAt);
}
const lastUnderscoreAt = name.lastIndexOf("_");
if (lastUnderscoreAt > -1) {
hash = name.slice(lastUnderscoreAt + 1);
name = name.slice(0, lastUnderscoreAt);
}
return { basename: name, hash, ext };
};

export const formatAssetName = (asset: Pick<Asset, "name" | "filename">) => {
const { basename, ext } = parseAssetName(asset.name);
const formattedName = `${asset.filename ?? basename}.${ext}`;
return formattedName;
};
2 changes: 1 addition & 1 deletion apps/builder/app/builder/shared/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Asset } from "@webstudio-is/sdk";

type PreviewAsset = Pick<
Asset,
"name" | "id" | "format" | "description" | "type"
"name" | "filename" | "id" | "format" | "description" | "type"
>;

export type UploadedAssetContainer = {
Expand Down
27 changes: 26 additions & 1 deletion apps/builder/app/builder/shared/assets/use-assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
UploadingAssetContainer,
} from "./types";
import {
formatAssetName,
getFileName,
getMimeType,
getSha256Hash,
Expand Down Expand Up @@ -253,6 +254,27 @@ const getVideoDimensions = async (file: File) => {
});
};

const deduplicateAssetName = (name: string) => {
const existingNames = new Set();
for (const asset of $assets.get().values()) {
existingNames.add(formatAssetName(asset));
}
// eslint-disable-next-line no-constant-condition
for (let index = 0; true; index += 1) {
const suffix = index === 0 ? "" : `_${index}`;
const lastDotAt = name.lastIndexOf(".");
if (lastDotAt === -1) {
return name;
}
const basename = name.slice(0, lastDotAt);
const ext = name.slice(lastDotAt);
const nameWithSuffix = basename + suffix + ext;
if (!existingNames.has(nameWithSuffix)) {
return nameWithSuffix;
}
}
};

const uploadAsset = async ({
authToken,
projectId,
Expand All @@ -275,7 +297,10 @@ const uploadAsset = async ({
metaFormData.append("type", mimeType);
// sanitizeS3Key here is just because of https://github.com/remix-run/remix/issues/4443
// should be removed after fix
metaFormData.append("filename", sanitizeS3Key(fileName));
metaFormData.append(
"filename",
deduplicateAssetName(sanitizeS3Key(fileName))
);

const authHeaders = new Headers();
if (authToken !== undefined) {
Expand Down
21 changes: 0 additions & 21 deletions apps/builder/app/builder/shared/image-manager/filename.tsx

This file was deleted.

Loading
Loading