Skip to content

Commit 2a1730c

Browse files
authored
Merge pull request #6 from Bebb-Protocol-and-Apps/bridge-preview
Bridge preview
2 parents 044ffc9 + 452948f commit 2a1730c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/bebb/entity.mo

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ module {
8484
public type EntityIdErrors = {
8585
#Unauthorized;
8686
#EntityNotFound;
87+
#TooManyPreviews;
88+
// Returns the index of the preview that is too large
89+
#PreviewTooLarge : Int;
8790
#Error;
8891
};
8992

@@ -125,6 +128,35 @@ module {
125128
#Location;
126129
};
127130

131+
/**
132+
* Stores the supported preview types
133+
* Standard types are enumerated to create a standarized preview format.
134+
* Unique formats can be stored via the other tag and labelled as such
135+
*/
136+
public type EntityPreviewSupportedTypes = {
137+
#Jpg;
138+
#Png;
139+
#Glb;
140+
#Gltf;
141+
#Other : Text;
142+
};
143+
144+
/**
145+
* Defines the type for the various previews of an entity
146+
*/
147+
public type EntityPreview = {
148+
/**
149+
* Stores the type of the preview. This is used to determine how to
150+
* render the stored preview data
151+
*/
152+
previewType: EntityPreviewSupportedTypes;
153+
154+
/**
155+
* The actual preview data associated with the preview
156+
*/
157+
previewData: Blob;
158+
};
159+
128160
/**
129161
* Type that defines the attributes for an Entity
130162
*/
@@ -147,6 +179,11 @@ module {
147179
* Contains all the bridge ids that point to this entity
148180
*/
149181
toIds : EntityAttachedBridges;
182+
183+
/**
184+
* Stores all the previews that are available for the current entity
185+
*/
186+
previews : [EntityPreview];
150187
};
151188

152189
/**
@@ -192,6 +229,7 @@ module {
192229
listOfEntitySpecificFieldKeys : [Text] = ["entityType", "fromIds", "toIds"];
193230
toIds : EntityAttachedBridges = [];
194231
fromIds : EntityAttachedBridges = [];
232+
previews : [EntityPreview] = [];
195233
};
196234
};
197235

@@ -215,6 +253,7 @@ module {
215253
listOfEntitySpecificFieldKeys = entity.listOfEntitySpecificFieldKeys;
216254
fromIds = fromIds;
217255
toIds = entity.toIds;
256+
previews = entity.previews;
218257
};
219258
};
220259

@@ -238,6 +277,7 @@ module {
238277
listOfEntitySpecificFieldKeys = entity.listOfEntitySpecificFieldKeys;
239278
fromIds = entity.fromIds;
240279
toIds = toIds;
280+
previews = entity.previews;
241281
};
242282
};
243283

@@ -247,7 +287,7 @@ module {
247287
*
248288
* @return The new entity with the values updated with the entity update values
249289
*/
250-
public func updateEntityFromUpdateObject(entityUpdateObject : EntityUpdateObject, originalEntity : Entity) : Entity {
290+
public func updateEntityFromUpdateObject(entityUpdateObject : EntityUpdateObject, originalEntity : Entity) : Entity {
251291
return {
252292
id = originalEntity.id;
253293
creationTimestamp = originalEntity.creationTimestamp;
@@ -256,12 +296,15 @@ module {
256296
settings = Option.get<EntitySettings>(entityUpdateObject.settings, originalEntity.settings);
257297
entityType = originalEntity.entityType;
258298
name = Option.get<?Text>(?entityUpdateObject.name, originalEntity.name);
299+
// TODO: This isn't working properly. If you keep description null when updating, it will set it back to null and
300+
// not keep it the same value. This is true for name as well. Preview seems to work as expected
259301
description : ?Text = Option.get<?Text>(?entityUpdateObject.description, originalEntity.description);
260302
keywords : ?[Text] = Option.get<?[Text]>(?entityUpdateObject.keywords, originalEntity.keywords);
261303
entitySpecificFields = originalEntity.entitySpecificFields;
262304
listOfEntitySpecificFieldKeys = originalEntity.listOfEntitySpecificFieldKeys;
263305
fromIds = originalEntity.fromIds;
264306
toIds = originalEntity.toIds;
307+
previews : [EntityPreview] = Option.get<[EntityPreview]>(entityUpdateObject.previews, originalEntity.previews);
265308
};
266309
};
267310

@@ -291,6 +334,10 @@ module {
291334
* The Updated keywords for the entity
292335
*/
293336
keywords : ?[Text];
337+
/**
338+
* Used to update the available previews for the entity
339+
*/
340+
previews : ?[EntityPreview]
294341
};
295342

296343
/**

src/bebb/main.mo

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ actor {
236236
*
237237
* @return Either the Entity ID if the call was successful or an error if not
238238
*/
239+
let oneMB : Nat = 1048576; // 1 MB
240+
private let maxPreviewBlobSize : Nat = 2 * oneMB;
241+
private let maxNumPreviews = 5;
239242
private func updateEntity(caller : Principal, entityUpdateObject : Entity.EntityUpdateObject) : async Entity.EntityIdResult {
240243
var entity = getEntity(entityUpdateObject.id);
241244
switch (entity) {
@@ -247,6 +250,32 @@ actor {
247250
};
248251
// Only owner may update the Entity
249252
case true {
253+
// Ensure that the preivews are not too large and that there aren't too many previews
254+
// If any of the previews are too large then return an error with
255+
// the preview index that caused the error of being too large
256+
var counter = 0;
257+
switch(entityUpdateObject.previews)
258+
{
259+
case(null) {};
260+
case(?new_previews)
261+
{
262+
// Check to ensure there aren't too many previews
263+
if (new_previews.size() > maxNumPreviews)
264+
{
265+
return #Err(#TooManyPreviews);
266+
};
267+
268+
// Check all the previews and make sure they aren't too big
269+
for (preview in new_previews.vals()) {
270+
let fileSize = preview.previewData.size();
271+
if (fileSize > maxPreviewBlobSize)
272+
{
273+
return #Err(#PreviewTooLarge(counter));
274+
};
275+
counter := counter + 1;
276+
};
277+
};
278+
};
250279
let updatedEntity : Entity.Entity = Entity.updateEntityFromUpdateObject(entityUpdateObject, entityToUpdate);
251280
let result = putEntity(updatedEntity);
252281
return #Ok(updatedEntity.id);

0 commit comments

Comments
 (0)