1package eu.siacs.conversations.persistance;
2
3import android.content.Intent;
4import android.graphics.Bitmap;
5import android.graphics.BitmapFactory;
6import android.graphics.Canvas;
7import android.graphics.Matrix;
8import android.graphics.RectF;
9import android.net.Uri;
10import android.os.Environment;
11import android.util.Base64;
12import android.util.Base64OutputStream;
13import android.util.Log;
14import android.webkit.MimeTypeMap;
15
16import java.io.ByteArrayOutputStream;
17import java.io.Closeable;
18import java.io.File;
19import java.io.FileNotFoundException;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.net.Socket;
25import java.net.URL;
26import java.security.DigestOutputStream;
27import java.security.MessageDigest;
28import java.security.NoSuchAlgorithmException;
29import java.text.SimpleDateFormat;
30import java.util.Date;
31import java.util.Locale;
32
33import eu.siacs.conversations.Config;
34import eu.siacs.conversations.R;
35import eu.siacs.conversations.entities.DownloadableFile;
36import eu.siacs.conversations.entities.Message;
37import eu.siacs.conversations.services.XmppConnectionService;
38import eu.siacs.conversations.utils.CryptoHelper;
39import eu.siacs.conversations.utils.ExifHelper;
40import eu.siacs.conversations.utils.FileUtils;
41import eu.siacs.conversations.xmpp.pep.Avatar;
42
43public class FileBackend {
44 private final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
45
46 private XmppConnectionService mXmppConnectionService;
47
48 public FileBackend(XmppConnectionService service) {
49 this.mXmppConnectionService = service;
50 }
51
52 private void createNoMedia() {
53 final File nomedia = new File(getConversationsFileDirectory()+".nomedia");
54 if (!nomedia.exists()) {
55 try {
56 nomedia.createNewFile();
57 } catch (Exception e) {
58 Log.d(Config.LOGTAG, "could not create nomedia file");
59 }
60 }
61 }
62
63 public void updateMediaScanner(File file) {
64 if (file.getAbsolutePath().startsWith(getConversationsImageDirectory())) {
65 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
66 intent.setData(Uri.fromFile(file));
67 mXmppConnectionService.sendBroadcast(intent);
68 } else {
69 createNoMedia();
70 }
71 }
72
73 public boolean deleteFile(Message message) {
74 File file = getFile(message);
75 if (file.delete()) {
76 updateMediaScanner(file);
77 return true;
78 } else {
79 return false;
80 }
81 }
82
83 public DownloadableFile getFile(Message message) {
84 return getFile(message, true);
85 }
86
87 public DownloadableFile getFile(Message message, boolean decrypted) {
88 final boolean encrypted = !decrypted
89 && (message.getEncryption() == Message.ENCRYPTION_PGP
90 || message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
91 final DownloadableFile file;
92 String path = message.getRelativeFilePath();
93 if (path == null) {
94 path = message.getUuid();
95 }
96 if (path.startsWith("/")) {
97 file = new DownloadableFile(path);
98 } else {
99 String mime = message.getMimeType();
100 if (mime != null && mime.startsWith("image")) {
101 file = new DownloadableFile(getConversationsImageDirectory() + path);
102 } else {
103 file = new DownloadableFile(getConversationsFileDirectory() + path);
104 }
105 }
106 if (encrypted) {
107 return new DownloadableFile(getConversationsFileDirectory() + file.getName() + ".pgp");
108 } else {
109 return file;
110 }
111 }
112
113 public static String getConversationsFileDirectory() {
114 return Environment.getExternalStorageDirectory().getAbsolutePath()+"/Conversations/";
115 }
116
117 public static String getConversationsImageDirectory() {
118 return Environment.getExternalStoragePublicDirectory(
119 Environment.DIRECTORY_PICTURES).getAbsolutePath()
120 + "/Conversations/";
121 }
122
123 public Bitmap resize(Bitmap originalBitmap, int size) {
124 int w = originalBitmap.getWidth();
125 int h = originalBitmap.getHeight();
126 if (Math.max(w, h) > size) {
127 int scalledW;
128 int scalledH;
129 if (w <= h) {
130 scalledW = (int) (w / ((double) h / size));
131 scalledH = size;
132 } else {
133 scalledW = size;
134 scalledH = (int) (h / ((double) w / size));
135 }
136 Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
137 if (originalBitmap != null && !originalBitmap.isRecycled()) {
138 originalBitmap.recycle();
139 }
140 return result;
141 } else {
142 return originalBitmap;
143 }
144 }
145
146 public static Bitmap rotate(Bitmap bitmap, int degree) {
147 if (degree == 0) {
148 return bitmap;
149 }
150 int w = bitmap.getWidth();
151 int h = bitmap.getHeight();
152 Matrix mtx = new Matrix();
153 mtx.postRotate(degree);
154 Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
155 if (bitmap != null && !bitmap.isRecycled()) {
156 bitmap.recycle();
157 }
158 return result;
159 }
160
161 public boolean useImageAsIs(Uri uri) {
162 String path = getOriginalPath(uri);
163 if (path == null) {
164 return false;
165 }
166 File file = new File(path);
167 long size = file.length();
168 if (size == 0 || size >= Config.IMAGE_MAX_SIZE ) {
169 return false;
170 }
171 BitmapFactory.Options options = new BitmapFactory.Options();
172 options.inJustDecodeBounds = true;
173 try {
174 BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
175 if (options == null || options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
176 return false;
177 }
178 return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
179 } catch (FileNotFoundException e) {
180 return false;
181 }
182 }
183
184 public String getOriginalPath(Uri uri) {
185 return FileUtils.getPath(mXmppConnectionService,uri);
186 }
187
188 public void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
189 file.getParentFile().mkdirs();
190 OutputStream os = null;
191 InputStream is = null;
192 try {
193 file.createNewFile();
194 os = new FileOutputStream(file);
195 is = mXmppConnectionService.getContentResolver().openInputStream(uri);
196 byte[] buffer = new byte[1024];
197 int length;
198 while ((length = is.read(buffer)) > 0) {
199 os.write(buffer, 0, length);
200 }
201 os.flush();
202 } catch(FileNotFoundException e) {
203 throw new FileCopyException(R.string.error_file_not_found);
204 } catch (IOException e) {
205 e.printStackTrace();
206 throw new FileCopyException(R.string.error_io_exception);
207 } finally {
208 close(os);
209 close(is);
210 }
211 Log.d(Config.LOGTAG, "output file name " + file.getAbsolutePath());
212 }
213
214 public void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
215 Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage");
216 String mime = mXmppConnectionService.getContentResolver().getType(uri);
217 String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
218 message.setRelativeFilePath(message.getUuid() + "." + extension);
219 copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
220 }
221
222 private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
223 file.getParentFile().mkdirs();
224 InputStream is = null;
225 OutputStream os = null;
226 try {
227 file.createNewFile();
228 is = mXmppConnectionService.getContentResolver().openInputStream(image);
229 Bitmap originalBitmap;
230 BitmapFactory.Options options = new BitmapFactory.Options();
231 int inSampleSize = (int) Math.pow(2, sampleSize);
232 Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
233 options.inSampleSize = inSampleSize;
234 originalBitmap = BitmapFactory.decodeStream(is, null, options);
235 is.close();
236 if (originalBitmap == null) {
237 throw new FileCopyException(R.string.error_not_an_image_file);
238 }
239 Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
240 int rotation = getRotation(image);
241 scaledBitmap = rotate(scaledBitmap, rotation);
242 boolean targetSizeReached = false;
243 int quality = Config.IMAGE_QUALITY;
244 while(!targetSizeReached) {
245 os = new FileOutputStream(file);
246 boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
247 if (!success) {
248 throw new FileCopyException(R.string.error_compressing_image);
249 }
250 os.flush();
251 targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50;
252 quality -= 5;
253 }
254 scaledBitmap.recycle();
255 return;
256 } catch (FileNotFoundException e) {
257 throw new FileCopyException(R.string.error_file_not_found);
258 } catch (IOException e) {
259 e.printStackTrace();
260 throw new FileCopyException(R.string.error_io_exception);
261 } catch (SecurityException e) {
262 throw new FileCopyException(R.string.error_security_exception_during_image_copy);
263 } catch (OutOfMemoryError e) {
264 ++sampleSize;
265 if (sampleSize <= 3) {
266 copyImageToPrivateStorage(file, image, sampleSize);
267 } else {
268 throw new FileCopyException(R.string.error_out_of_memory);
269 }
270 } catch (NullPointerException e) {
271 throw new FileCopyException(R.string.error_io_exception);
272 } finally {
273 close(os);
274 close(is);
275 }
276 }
277
278 public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
279 copyImageToPrivateStorage(file, image, 0);
280 }
281
282 public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
283 switch(Config.IMAGE_FORMAT) {
284 case JPEG:
285 message.setRelativeFilePath(message.getUuid()+".jpg");
286 break;
287 case PNG:
288 message.setRelativeFilePath(message.getUuid()+".png");
289 break;
290 case WEBP:
291 message.setRelativeFilePath(message.getUuid()+".webp");
292 break;
293 }
294 copyImageToPrivateStorage(getFile(message), image);
295 updateFileParams(message);
296 }
297
298 private int getRotation(File file) {
299 return getRotation(Uri.parse("file://"+file.getAbsolutePath()));
300 }
301
302 private int getRotation(Uri image) {
303 InputStream is = null;
304 try {
305 is = mXmppConnectionService.getContentResolver().openInputStream(image);
306 return ExifHelper.getOrientation(is);
307 } catch (FileNotFoundException e) {
308 return 0;
309 } finally {
310 close(is);
311 }
312 }
313
314 public Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
315 throws FileNotFoundException {
316 Bitmap thumbnail = mXmppConnectionService.getBitmapCache().get(message.getUuid());
317 if ((thumbnail == null) && (!cacheOnly)) {
318 File file = getFile(message);
319 BitmapFactory.Options options = new BitmapFactory.Options();
320 options.inSampleSize = calcSampleSize(file, size);
321 Bitmap fullsize = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
322 if (fullsize == null) {
323 throw new FileNotFoundException();
324 }
325 thumbnail = resize(fullsize, size);
326 thumbnail = rotate(thumbnail, getRotation(file));
327 this.mXmppConnectionService.getBitmapCache().put(message.getUuid(),thumbnail);
328 }
329 return thumbnail;
330 }
331
332 public Uri getTakePhotoUri() {
333 StringBuilder pathBuilder = new StringBuilder();
334 pathBuilder.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
335 pathBuilder.append('/');
336 pathBuilder.append("Camera");
337 pathBuilder.append('/');
338 pathBuilder.append("IMG_" + this.imageDateFormat.format(new Date()) + ".jpg");
339 Uri uri = Uri.parse("file://" + pathBuilder.toString());
340 File file = new File(uri.toString());
341 file.getParentFile().mkdirs();
342 return uri;
343 }
344
345 public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
346 try {
347 Avatar avatar = new Avatar();
348 Bitmap bm = cropCenterSquare(image, size);
349 if (bm == null) {
350 return null;
351 }
352 ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
353 Base64OutputStream mBase64OutputSttream = new Base64OutputStream(
354 mByteArrayOutputStream, Base64.DEFAULT);
355 MessageDigest digest = MessageDigest.getInstance("SHA-1");
356 DigestOutputStream mDigestOutputStream = new DigestOutputStream(
357 mBase64OutputSttream, digest);
358 if (!bm.compress(format, 75, mDigestOutputStream)) {
359 return null;
360 }
361 mDigestOutputStream.flush();
362 mDigestOutputStream.close();
363 avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
364 avatar.image = new String(mByteArrayOutputStream.toByteArray());
365 return avatar;
366 } catch (NoSuchAlgorithmException e) {
367 return null;
368 } catch (IOException e) {
369 return null;
370 }
371 }
372
373 public boolean isAvatarCached(Avatar avatar) {
374 File file = new File(getAvatarPath(avatar.getFilename()));
375 return file.exists();
376 }
377
378 public boolean save(Avatar avatar) {
379 File file;
380 if (isAvatarCached(avatar)) {
381 file = new File(getAvatarPath(avatar.getFilename()));
382 } else {
383 String filename = getAvatarPath(avatar.getFilename());
384 file = new File(filename + ".tmp");
385 file.getParentFile().mkdirs();
386 OutputStream os = null;
387 try {
388 file.createNewFile();
389 os = new FileOutputStream(file);
390 MessageDigest digest = MessageDigest.getInstance("SHA-1");
391 digest.reset();
392 DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
393 mDigestOutputStream.write(avatar.getImageAsBytes());
394 mDigestOutputStream.flush();
395 mDigestOutputStream.close();
396 String sha1sum = CryptoHelper.bytesToHex(digest.digest());
397 if (sha1sum.equals(avatar.sha1sum)) {
398 file.renameTo(new File(filename));
399 } else {
400 Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
401 file.delete();
402 return false;
403 }
404 } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
405 return false;
406 } finally {
407 close(os);
408 }
409 }
410 avatar.size = file.length();
411 return true;
412 }
413
414 public String getAvatarPath(String avatar) {
415 return mXmppConnectionService.getFilesDir().getAbsolutePath()+ "/avatars/" + avatar;
416 }
417
418 public Uri getAvatarUri(String avatar) {
419 return Uri.parse("file:" + getAvatarPath(avatar));
420 }
421
422 public Bitmap cropCenterSquare(Uri image, int size) {
423 if (image == null) {
424 return null;
425 }
426 InputStream is = null;
427 try {
428 BitmapFactory.Options options = new BitmapFactory.Options();
429 options.inSampleSize = calcSampleSize(image, size);
430 is = mXmppConnectionService.getContentResolver().openInputStream(image);
431 if (is == null) {
432 return null;
433 }
434 Bitmap input = BitmapFactory.decodeStream(is, null, options);
435 if (input == null) {
436 return null;
437 } else {
438 input = rotate(input, getRotation(image));
439 return cropCenterSquare(input, size);
440 }
441 } catch (SecurityException e) {
442 return null; // happens for example on Android 6.0 if contacts permissions get revoked
443 } catch (FileNotFoundException e) {
444 return null;
445 } finally {
446 close(is);
447 }
448 }
449
450 public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
451 if (image == null) {
452 return null;
453 }
454 InputStream is = null;
455 try {
456 BitmapFactory.Options options = new BitmapFactory.Options();
457 options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
458 is = mXmppConnectionService.getContentResolver().openInputStream(image);
459 if (is == null) {
460 return null;
461 }
462 Bitmap source = BitmapFactory.decodeStream(is, null, options);
463 if (source == null) {
464 return null;
465 }
466 int sourceWidth = source.getWidth();
467 int sourceHeight = source.getHeight();
468 float xScale = (float) newWidth / sourceWidth;
469 float yScale = (float) newHeight / sourceHeight;
470 float scale = Math.max(xScale, yScale);
471 float scaledWidth = scale * sourceWidth;
472 float scaledHeight = scale * sourceHeight;
473 float left = (newWidth - scaledWidth) / 2;
474 float top = (newHeight - scaledHeight) / 2;
475
476 RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
477 Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
478 Canvas canvas = new Canvas(dest);
479 canvas.drawBitmap(source, null, targetRect, null);
480 if (source != null && !source.isRecycled()) {
481 source.recycle();
482 }
483 return dest;
484 } catch (SecurityException e) {
485 return null; //android 6.0 with revoked permissions for example
486 } catch (FileNotFoundException e) {
487 return null;
488 } finally {
489 close(is);
490 }
491 }
492
493 public Bitmap cropCenterSquare(Bitmap input, int size) {
494 int w = input.getWidth();
495 int h = input.getHeight();
496
497 float scale = Math.max((float) size / h, (float) size / w);
498
499 float outWidth = scale * w;
500 float outHeight = scale * h;
501 float left = (size - outWidth) / 2;
502 float top = (size - outHeight) / 2;
503 RectF target = new RectF(left, top, left + outWidth, top + outHeight);
504
505 Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
506 Canvas canvas = new Canvas(output);
507 canvas.drawBitmap(input, null, target, null);
508 if (input != null && !input.isRecycled()) {
509 input.recycle();
510 }
511 return output;
512 }
513
514 private int calcSampleSize(Uri image, int size) throws FileNotFoundException, SecurityException {
515 BitmapFactory.Options options = new BitmapFactory.Options();
516 options.inJustDecodeBounds = true;
517 BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(image), null, options);
518 return calcSampleSize(options, size);
519 }
520
521 private static int calcSampleSize(File image, int size) {
522 BitmapFactory.Options options = new BitmapFactory.Options();
523 options.inJustDecodeBounds = true;
524 BitmapFactory.decodeFile(image.getAbsolutePath(), options);
525 return calcSampleSize(options, size);
526 }
527
528 public static int calcSampleSize(BitmapFactory.Options options, int size) {
529 int height = options.outHeight;
530 int width = options.outWidth;
531 int inSampleSize = 1;
532
533 if (height > size || width > size) {
534 int halfHeight = height / 2;
535 int halfWidth = width / 2;
536
537 while ((halfHeight / inSampleSize) > size
538 && (halfWidth / inSampleSize) > size) {
539 inSampleSize *= 2;
540 }
541 }
542 return inSampleSize;
543 }
544
545 public Uri getJingleFileUri(Message message) {
546 File file = getFile(message);
547 return Uri.parse("file://" + file.getAbsolutePath());
548 }
549
550 public void updateFileParams(Message message) {
551 updateFileParams(message,null);
552 }
553
554 public void updateFileParams(Message message, URL url) {
555 DownloadableFile file = getFile(message);
556 if (message.getType() == Message.TYPE_IMAGE || file.getMimeType().startsWith("image/")) {
557 BitmapFactory.Options options = new BitmapFactory.Options();
558 options.inJustDecodeBounds = true;
559 BitmapFactory.decodeFile(file.getAbsolutePath(), options);
560 int rotation = getRotation(file);
561 boolean rotated = rotation == 90 || rotation == 270;
562 int imageHeight = rotated ? options.outWidth : options.outHeight;
563 int imageWidth = rotated ? options.outHeight : options.outWidth;
564 if (url == null) {
565 message.setBody(Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
566 } else {
567 message.setBody(url.toString()+"|"+Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
568 }
569 } else {
570 if (url != null) {
571 message.setBody(url.toString()+"|"+Long.toString(file.getSize()));
572 } else {
573 message.setBody(Long.toString(file.getSize()));
574 }
575 }
576
577 }
578
579 public class FileCopyException extends Exception {
580 private static final long serialVersionUID = -1010013599132881427L;
581 private int resId;
582
583 public FileCopyException(int resId) {
584 this.resId = resId;
585 }
586
587 public int getResId() {
588 return resId;
589 }
590 }
591
592 public Bitmap getAvatar(String avatar, int size) {
593 if (avatar == null) {
594 return null;
595 }
596 Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
597 if (bm == null) {
598 return null;
599 }
600 return bm;
601 }
602
603 public boolean isFileAvailable(Message message) {
604 return getFile(message).exists();
605 }
606
607 public static void close(Closeable stream) {
608 if (stream != null) {
609 try {
610 stream.close();
611 } catch (IOException e) {
612 }
613 }
614 }
615
616 public static void close(Socket socket) {
617 if (socket != null) {
618 try {
619 socket.close();
620 } catch (IOException e) {
621 }
622 }
623 }
624}