FileBackend.java

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