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