Skip to content

Commit

Permalink
Merge pull request #32 from prtcl/spike/image-size
Browse files Browse the repository at this point in the history
spike/image size
  • Loading branch information
prtcl authored Nov 12, 2024
2 parents 25556c4 + 4446553 commit effed1d
Show file tree
Hide file tree
Showing 15 changed files with 788 additions and 399 deletions.
7 changes: 7 additions & 0 deletions convex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"node": {
"externalPackages": [
"image-size"
]
}
}
10 changes: 6 additions & 4 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import type {
FilterApi,
FunctionReference,
} from "convex/server";
import type * as details from "../details.js";
import type * as features from "../features.js";
import type * as previews from "../previews.js";
import type * as imageMigration from "../imageMigration.js";
import type * as internal_ from "../internal.js";
import type * as migrations from "../migrations.js";
import type * as projects from "../projects.js";

/**
Expand All @@ -27,9 +28,10 @@ import type * as projects from "../projects.js";
* ```
*/
declare const fullApi: ApiFromModules<{
details: typeof details;
features: typeof features;
previews: typeof previews;
imageMigration: typeof imageMigration;
internal: typeof internal_;
migrations: typeof migrations;
projects: typeof projects;
}>;
export declare const api: FilterApi<
Expand Down
161 changes: 0 additions & 161 deletions convex/details.ts

This file was deleted.

119 changes: 119 additions & 0 deletions convex/imageMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
'use node';

import sizeOf from 'image-size';
import { internal } from './_generated/api';
import type { Id } from './_generated/dataModel';
import { internalAction } from './_generated/server';

type UpdatePayload = {
coverImageId: Id<'images'> | null;
previewImageId: Id<'images'> | null;
projectId: Id<'projects'>;
};

export const calculateImageDimensions = internalAction({
args: {},
handler: async (ctx) => {
const details = await ctx.runQuery(internal.migrations.collectAllDetails);
const previews = await ctx.runQuery(internal.migrations.collectAllPreviews);
const projectIds = new Set<Id<'projects'>>([
...details.map((d) => d.projectId),
...previews.map((p) => p.projectId),
]);
const updates: Map<Id<'projects'>, UpdatePayload> = new Map();
const res: Id<'projects'>[] = [];

for (const projectId of projectIds) {
updates.set(projectId, {
coverImageId: null,
previewImageId: null,
projectId,
});
}

for (const detail of details) {
const { coverImageId: initialStorageId, projectId } = detail;

if (!initialStorageId) {
continue;
}

const storageItem = await ctx.storage.get(initialStorageId);
const payload = updates.get(detail.projectId);

if (!storageItem || !payload) {
throw new Error(`Cannot find storage item: ${initialStorageId}`);
}

const buffer = await storageItem.arrayBuffer();
const dimensions = sizeOf(Buffer.from(buffer));

console.log({ dimensions, storageItem, detail });

Check warning on line 51 in convex/imageMigration.ts

View workflow job for this annotation

GitHub Actions / deploy

Unexpected console statement

if (!dimensions.width || !dimensions.height) {
throw new Error(`Cannot calculate dimensions: ${initialStorageId}`);
}

const insertedImageId = await ctx.runMutation(
internal.migrations.createImage,
{
mimeType: storageItem.type,
naturalHeight: dimensions.width,
naturalWidth: dimensions.height,
size: storageItem.size,
storageId: initialStorageId,
},
);

updates.set(projectId, {
...payload,
coverImageId: insertedImageId,
});
}

for (const preview of previews) {
const { storageId: initialStorageId, projectId } = preview;
const payload = updates.get(projectId);
const storageItem = await ctx.storage.get(initialStorageId);

if (!storageItem || !payload) {
throw new Error(`Cannot find storage item: ${initialStorageId}`);
}

const buffer = await storageItem.arrayBuffer();
const dimensions = sizeOf(Buffer.from(buffer));

console.log({ dimensions, storageItem, preview });

Check warning on line 86 in convex/imageMigration.ts

View workflow job for this annotation

GitHub Actions / deploy

Unexpected console statement

if (!dimensions.width || !dimensions.height) {
throw new Error(`Cannot calculate dimensions: ${initialStorageId}`);
}

const insertedImageId = await ctx.runMutation(
internal.migrations.createImage,
{
mimeType: storageItem.type,
naturalHeight: dimensions.width,
naturalWidth: dimensions.height,
size: storageItem.size,
storageId: initialStorageId,
},
);

updates.set(projectId, {
...payload,
previewImageId: insertedImageId,
});
}

for (const [projectId, payload] of updates.entries()) {
await ctx.runMutation(internal.migrations.updateProjectImages, {
...payload,
});

res.push(projectId);
}

return res;
},
});
Loading

0 comments on commit effed1d

Please sign in to comment.