MCPcopy Create free account
hub / github.com/M66B/FairEmail / get

Method get

app/src/main/java/eu/faircode/email/OpenAI.java:281–333  ·  view source on GitHub ↗
(Spannable ssb, long id, Context context)

Source from the content-addressed store, hash-verified

279 }
280
281 static Content[] get(Spannable ssb, long id, Context context) {
282 DB db = DB.getInstance(context);
283 List<OpenAI.Content> contents = new ArrayList<>();
284 int start = 0;
285 while (start < ssb.length()) {
286 int end = ssb.nextSpanTransition(start, ssb.length(), ImageSpanEx.class);
287
288 String text = ssb.subSequence(start, end).toString().trim()
289 .replace("\u00a0", "")
290 .replace("\ufffc", "");
291 Log.i("OpenAI content " + start + "..." + end +
292 " text=[" + Helper.getPrintableString(text, true) + "]");
293
294 if (!TextUtils.isEmpty(text))
295 contents.add(new OpenAI.Content(OpenAI.CONTENT_TEXT, text));
296
297 if (end < ssb.length()) {
298 ImageSpanEx[] spans = ssb.getSpans(end, end, ImageSpanEx.class);
299 Log.i("OpenAI images=" + (spans == null ? null : spans.length));
300 if (spans != null && spans.length == 1) {
301 int e = ssb.getSpanEnd(spans[0]);
302
303 String url = null;
304 String src = spans[0].getSource();
305 Log.i("OpenAI image url=" + src);
306 if (src != null && src.startsWith("cid:")) {
307 String cid = '<' + src.substring(4) + '>';
308 EntityAttachment attachment = db.attachment().getAttachment(id, cid);
309 if (attachment != null && attachment.available) {
310 File file = attachment.getFile(context);
311 try (InputStream is = new FileInputStream(file)) {
312 Bitmap bm = ImageHelper.getScaledBitmap(is, null, null, SCALE2PIXELS);
313 Helper.ByteArrayInOutStream bos = new Helper.ByteArrayInOutStream();
314 bm.compress(Bitmap.CompressFormat.PNG, ImageHelper.DEFAULT_PNG_COMPRESSION, bos);
315 url = ImageHelper.getDataUri(bos.getInputStream(), "image/png");
316 } catch (Throwable ex) {
317 Log.w(ex);
318 }
319 }
320 } else
321 url = src;
322 if (url != null)
323 contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url));
324
325 end = e;
326 }
327 }
328
329 start = (end > start ? end : start + 1);
330 }
331
332 return contents.toArray(new OpenAI.Content[0]);
333 }
334 }
335
336 static class Message {

Callers 1

checkModerationMethod · 0.45

Calls 15

getInstanceMethod · 0.95
iMethod · 0.95
getPrintableStringMethod · 0.95
attachmentMethod · 0.95
getFileMethod · 0.95
getScaledBitmapMethod · 0.95
getDataUriMethod · 0.95
getInputStreamMethod · 0.95
wMethod · 0.95
trimMethod · 0.80
getSourceMethod · 0.80
getAttachmentMethod · 0.80

Tested by

no test coverage detected