FileBackend.java

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