aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Import & Export/ImageUtils.ts
diff options
context:
space:
mode:
authorSophie Zhang <sophie_zhang@brown.edu>2024-01-25 11:35:26 -0500
committerSophie Zhang <sophie_zhang@brown.edu>2024-01-25 11:35:26 -0500
commitf3dab2a56db5e4a6a3dca58185d94e1ff7d1dc32 (patch)
treea7bc895266b53bb620dbd2dd71bad2e83b555446 /src/client/util/Import & Export/ImageUtils.ts
parentb5c5410b4af5d2c68d2107d3f064f6e3ec4ac3f2 (diff)
parent136f3d9f349d54e8bdd73b6380ea47c19e5edebf (diff)
Merge branch 'master' into sophie-ai-images
Diffstat (limited to 'src/client/util/Import & Export/ImageUtils.ts')
-rw-r--r--src/client/util/Import & Export/ImageUtils.ts41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/client/util/Import & Export/ImageUtils.ts b/src/client/util/Import & Export/ImageUtils.ts
index 55d37f544..d99828956 100644
--- a/src/client/util/Import & Export/ImageUtils.ts
+++ b/src/client/util/Import & Export/ImageUtils.ts
@@ -4,28 +4,33 @@ import { Cast, StrCast, NumCast } from '../../../fields/Types';
import { Networking } from '../../Network';
import { Id } from '../../../fields/FieldSymbols';
import { Utils } from '../../../Utils';
+import { DocData } from '../../../fields/DocSymbols';
export namespace ImageUtils {
- export const ExtractExif = async (document: Doc): Promise<boolean> => {
+ export type imgInfo = {
+ contentSize: number;
+ nativeWidth: number;
+ nativeHeight: number;
+ source: string;
+ exifData: { error: string | undefined; data: string };
+ };
+ export const ExtractImgInfo = async (document: Doc): Promise<imgInfo | undefined> => {
const field = Cast(document.data, ImageField);
- if (!field) {
- return false;
+ return field ? await Networking.PostToServer('/inspectImage', { source: field.url.href }) : undefined;
+ };
+
+ export const AssignImgInfo = (document: Doc, data?: imgInfo) => {
+ if (data) {
+ data.nativeWidth && (document._height = (NumCast(document._width) * data.nativeHeight) / data.nativeWidth);
+ const proto = document[DocData];
+ const field = Doc.LayoutFieldKey(document);
+ proto[`${field}_nativeWidth`] = data.nativeWidth;
+ proto[`${field}_nativeHeight`] = data.nativeHeight;
+ proto[`${field}_path`] = data.source;
+ proto[`${field}_exif`] = JSON.stringify(data.exifData.data);
+ proto[`${field}_contentSize`] = data.contentSize ? data.contentSize : undefined;
}
- const source = field.url.href;
- const {
- contentSize,
- nativeWidth,
- nativeHeight,
- exifData: { error, data },
- } = await Networking.PostToServer('/inspectImage', { source });
- document.exif = error || Doc.Get.FromJson({ data });
- const proto = Doc.GetProto(document);
- nativeWidth && (document._height = (NumCast(document._width) * nativeHeight) / nativeWidth);
- proto['data_nativeWidth'] = nativeWidth;
- proto['data_nativeHeight'] = nativeHeight;
- proto['data-path'] = source;
- proto.data_contentSize = contentSize ? contentSize : undefined;
- return data !== undefined;
+ return document;
};
export const ExportHierarchyToFileSystem = async (collection: Doc): Promise<void> => {