Skip to content

Commit 81ad324

Browse files
committed
dev: entity storage parameters + app icon size
1 parent f032faa commit 81ad324

File tree

4 files changed

+79
-43
lines changed

4 files changed

+79
-43
lines changed

src/backend/src/om/entitystorage/AppES.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const { app_name_exists, get_user, refresh_apps_cache } = require("../../helpers
2222
const { AppUnderUserActorType } = require("../../services/auth/Actor");
2323
const { DB_WRITE } = require("../../services/database/consts");
2424
const { Context } = require("../../util/context");
25+
const { stream_to_buffer } = require("../../util/streamutil");
2526
const { origin_from_url } = require("../../util/urlutil");
2627
const { Eq, Like, Or } = require("../query/query");
2728
const { BaseES } = require("./BaseES");
@@ -226,6 +227,27 @@ class AppES extends BaseES {
226227
entity.del('approved_for_opening_items');
227228
entity.del('approved_for_incentive_program');
228229
}
230+
231+
// Replace icon if an icon size is specified
232+
const icon_size = Context.get('es_params')?.icon_size;
233+
if ( icon_size ) {
234+
console.log('GOING TO');
235+
const svc_appIcon = this.context.get('services').get('app-icon');
236+
try {
237+
const stream = await svc_appIcon.get_icon_stream({
238+
app_uid: await entity.get('uid'),
239+
size: icon_size,
240+
});
241+
if ( ! stream ) throw Error('no stream');
242+
const buffer = await stream_to_buffer(stream);
243+
const data_url = `data:image/png;base64,${buffer.toString('base64')}`;
244+
await entity.set('icon', data_url);
245+
console.log('DID IT')
246+
} catch (e) {
247+
const svc_error = this.context.get('services').get('error-service');
248+
svc_error.report('AppES:read_transform', { source: e });
249+
}
250+
}
229251
},
230252
async maybe_insert_subdomain_ (entity) {
231253
// Create and update is a situation where we might create a subdomain

src/backend/src/services/AppIconService.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,41 @@ class AppIconService extends BaseService {
5151
app_uid = `app-${app_uid}`;
5252
}
5353

54-
// Get icon file node
55-
const dir_app_icons = await this.get_app_icons();
56-
const node = await dir_app_icons.getChild(`${app_uid}-${size}.png`);
57-
if ( ! await node.exists() ) {
58-
// Use database-stored icon as a fallback
59-
const app = await get_app({ uid: app_uid });
60-
if ( ! app.icon ) {
61-
app.icon = DEFAULT_APP_ICON;
62-
}
63-
const [metadata, app_icon] = app.icon.split(',');
64-
console.log('METADATA', metadata);
65-
const mime = metadata.split(';')[0].split(':')[1];
66-
const img = Buffer.from(app_icon, 'base64');
67-
res.set('Content-Type', mime);
68-
res.send(img);
69-
return;
70-
}
71-
72-
const svc_su = this.services.get('su');
73-
const ll_read = new LLRead();
74-
const stream = await ll_read.run({
75-
fsNode: node,
76-
actor: await svc_su.get_system_actor(),
77-
});
54+
const stream = await this.get_icon_stream({ app_uid, size, })
7855

7956
res.set('Content-Type', 'image/png');
8057
stream.pipe(res);
8158
},
8259
}).attach(app);
8360
}
8461

62+
async get_icon_stream ({ app_uid, size }) {
63+
// Get icon file node
64+
const dir_app_icons = await this.get_app_icons();
65+
const node = await dir_app_icons.getChild(`${app_uid}-${size}.png`);
66+
if ( ! await node.exists() ) {
67+
// Use database-stored icon as a fallback
68+
const app = await get_app({ uid: app_uid });
69+
if ( ! app.icon ) {
70+
app.icon = DEFAULT_APP_ICON;
71+
}
72+
const [metadata, app_icon] = app.icon.split(',');
73+
console.log('METADATA', metadata);
74+
const mime = metadata.split(';')[0].split(':')[1];
75+
const img = Buffer.from(app_icon, 'base64');
76+
res.set('Content-Type', mime);
77+
res.send(img);
78+
return;
79+
}
80+
81+
const svc_su = this.services.get('su');
82+
const ll_read = new LLRead();
83+
return await ll_read.run({
84+
fsNode: node,
85+
actor: await svc_su.get_system_actor(),
86+
});
87+
}
88+
8589
/**
8690
* Returns an FSNodeContext instance for the app icons
8791
* directory.

src/backend/src/services/EntityStoreService.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,36 @@ class EntityStoreService extends BaseService {
9191
const entity = await Entity.create({ om: this.om }, object);
9292
return await this.upsert(entity, id, options);
9393
},
94-
async read ({ uid, id }) {
95-
if ( ! uid && ! id ) {
96-
throw APIError.create('xor_field_missing', null, {
97-
names: ['uid', 'id'],
98-
});
99-
}
94+
async read ({ uid, id, params = {} }) {
95+
return await Context.sub({
96+
es_params: params,
97+
}).arun(async () => {
98+
if ( ! uid && ! id ) {
99+
throw APIError.create('xor_field_missing', null, {
100+
names: ['uid', 'id'],
101+
});
102+
}
100103

101-
const entity = await this.fetch_based_on_either_id_(uid, id);
102-
if ( ! entity ) {
103-
throw APIError.create('entity_not_found', null, {
104-
identifier: uid
105-
});
106-
}
107-
return await entity.get_client_safe();
104+
const entity = await this.fetch_based_on_either_id_(uid, id);
105+
if ( ! entity ) {
106+
throw APIError.create('entity_not_found', null, {
107+
identifier: uid
108+
});
109+
}
110+
return await entity.get_client_safe();
111+
});
108112
},
109113
async select (options) {
110-
const entities = await this.select(options);
111-
const client_safe_entities = [];
112-
for ( const entity of entities ) {
113-
client_safe_entities.push(await entity.get_client_safe());
114-
}
115-
return client_safe_entities;
114+
return await Context.sub({
115+
es_params: options?.params ?? {},
116+
}).arun(async () => {
117+
const entities = await this.select(options);
118+
const client_safe_entities = [];
119+
for ( const entity of entities ) {
120+
client_safe_entities.push(await entity.get_client_safe());
121+
}
122+
return client_safe_entities;
123+
});
116124
},
117125
async delete ({ uid, id }) {
118126
if ( ! uid && ! id ) {

src/backend/src/services/drivers/interfaces.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ const ENTITY_STORAGE_INTERFACE = {
3333
parameters: {
3434
uid: { type: 'string' },
3535
id: { type: 'json' },
36+
params: { type: 'json' },
3637
}
3738
},
3839
select: {
3940
parameters: {
4041
predicate: { type: 'json' },
4142
offset: { type: 'number' },
4243
limit: { type: 'number' },
44+
params: { type: 'json' },
4345
}
4446
},
4547
update: {

0 commit comments

Comments
 (0)