Search in sources :

Example 6 with File

use of info.guardianproject.iocipher.File in project Zom-Android by zom.

the class IOCipherOmemoStore method removeAllRawSessionsOf.

@Override
public void removeAllRawSessionsOf(OmemoManager omemoManager, BareJid contact) {
    File contactsDirectory = hierarchy.getContactsDir(omemoManager, contact);
    String[] devices = contactsDirectory.list();
    for (String deviceId : devices != null ? devices : new String[0]) {
        int id;
        try {
            id = Integer.parseInt(deviceId);
        } catch (NumberFormatException e) {
            continue;
        }
        OmemoDevice device = new OmemoDevice(contact, id);
        File session = hierarchy.getContactsSessionPath(omemoManager, device);
        session.delete();
    }
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) File(info.guardianproject.iocipher.File) OmemoFingerprint(org.jivesoftware.smackx.omemo.OmemoFingerprint)

Example 7 with File

use of info.guardianproject.iocipher.File in project Zom-Android by zom.

the class Downloader method openSecureStorageFile.

public File openSecureStorageFile(String sessionId, String url) throws FileNotFoundException {
    // debug( "openFile: url " + url) ;
    String filename = getFilenameFromUrl(url);
    String localFilename = SecureMediaStore.getDownloadFilename(sessionId, filename);
    // debug( "openFile: localFilename " + localFilename) ;
    info.guardianproject.iocipher.File fileNew = new info.guardianproject.iocipher.File(localFilename);
    fileNew.getParentFile().mkdirs();
    return fileNew;
}
Also used : File(info.guardianproject.iocipher.File) File(info.guardianproject.iocipher.File)

Example 8 with File

use of info.guardianproject.iocipher.File in project Zom-Android by zom.

the class OtrDataHandler method onIncomingRequest.

public synchronized void onIncomingRequest(Address requestThem, Address requestUs, byte[] value) {
    // Log.e( TAG, "onIncomingRequest:" + requestThem);
    SessionInputBuffer inBuf = new MemorySessionInputBuffer(value);
    HttpRequestParser parser = new HttpRequestParser(inBuf, lineParser, requestFactory, params);
    HttpRequest req;
    try {
        req = (HttpRequest) parser.parse();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (HttpException e) {
        e.printStackTrace();
        return;
    }
    String requestMethod = req.getRequestLine().getMethod();
    String uid = req.getFirstHeader("Request-Id").getValue();
    String url = req.getRequestLine().getUri();
    if (requestMethod.equals("OFFER")) {
        debug("incoming OFFER " + url);
        for (Header header : req.getAllHeaders()) {
            debug("incoming header: " + header.getName() + "=" + header.getValue());
        }
        if (!url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
            debug("Unknown url scheme " + url);
            sendResponse(requestUs, requestThem, 400, "Unknown scheme", uid, EMPTY_BODY);
            return;
        }
        if (!req.containsHeader("File-Length")) {
            sendResponse(requestUs, requestThem, 400, "File-Length must be supplied", uid, EMPTY_BODY);
            return;
        }
        int length = Integer.parseInt(req.getFirstHeader("File-Length").getValue());
        if (!req.containsHeader("File-Hash-SHA1")) {
            sendResponse(requestUs, requestThem, 400, "File-Hash-SHA1 must be supplied", uid, EMPTY_BODY);
            return;
        }
        sendResponse(requestUs, requestThem, 200, "OK", uid, EMPTY_BODY);
        String sum = req.getFirstHeader("File-Hash-SHA1").getValue();
        String type = null;
        if (req.containsHeader("Mime-Type")) {
            type = req.getFirstHeader("Mime-Type").getValue();
        }
        debug("Incoming sha1sum " + sum);
        Transfer transfer;
        try {
            transfer = new VfsTransfer(url, type, length, requestUs, requestThem, sum);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        transferCache.put(url, transfer);
        // Handle offer
        // TODO ask user to confirm we want this
        boolean accept = false;
        if (mDataListener != null) {
            try {
                mDataListener.onTransferRequested(url, requestThem.getAddress(), requestUs.getAddress(), transfer.url);
            // callback is now async, via "acceptTransfer" method
            // if (accept)
            // transfer.perform();
            } catch (RemoteException e) {
                LogCleaner.error(ImApp.LOG_TAG, "error approving OTRDATA transfer request", e);
            }
        }
    } else if (requestMethod.equals("GET") && url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
        debug("incoming GET " + url);
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int reqEnd;
        try {
            Offer offer = offerCache.get(url);
            if (offer == null) {
                sendResponse(requestUs, requestThem, 400, "No such offer made", uid, EMPTY_BODY);
                return;
            }
            // in case we don't see a response to underlying request, but peer still proceeds
            offer.seen();
            if (!req.containsHeader("Range")) {
                sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
                return;
            }
            String rangeHeader = req.getFirstHeader("Range").getValue();
            String[] spec = rangeHeader.split("=");
            if (spec.length != 2 || !spec[0].equals("bytes")) {
                sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
                return;
            }
            String[] startEnd = spec[1].split("-");
            if (startEnd.length != 2) {
                sendResponse(requestUs, requestThem, 400, "Range must be START-END", uid, EMPTY_BODY);
                return;
            }
            int start = Integer.parseInt(startEnd[0]);
            int end = Integer.parseInt(startEnd[1]);
            if (end - start + 1 > MAX_CHUNK_LENGTH) {
                sendResponse(requestUs, requestThem, 400, "Range must be at most " + MAX_CHUNK_LENGTH, uid, EMPTY_BODY);
                return;
            }
            File fileGet = new File(offer.getUri());
            long fileLength = -1;
            if (fileGet.exists()) {
                fileLength = fileGet.length();
                FileInputStream is = new FileInputStream(fileGet);
                readIntoByteBuffer(byteBuffer, is, start, end);
                is.close();
            } else {
                java.io.File fileGetExtern = new java.io.File(offer.getUri());
                if (fileGetExtern.exists()) {
                    fileLength = fileGetExtern.length();
                    java.io.FileInputStream is = new java.io.FileInputStream(fileGetExtern);
                    readIntoByteBuffer(byteBuffer, is, start, end);
                    is.close();
                }
            }
            if (mDataListener != null && fileLength != -1) {
                float percent = ((float) end) / ((float) fileLength);
                mDataListener.onTransferProgress(true, offer.getId(), requestThem.getAddress(), offer.getUri(), percent);
                String mimeType = null;
                if (req.getFirstHeader("Mime-Type") != null)
                    mimeType = req.getFirstHeader("Mime-Type").getValue();
                mDataListener.onTransferComplete(true, offer.getId(), requestThem.getAddress(), offer.getUri(), mimeType, offer.getUri());
            }
        } catch (UnsupportedEncodingException e) {
            // throw new RuntimeException(e);
            sendResponse(requestUs, requestThem, 400, "Unsupported encoding", uid, EMPTY_BODY);
            return;
        } catch (IOException e) {
            // throw new RuntimeException(e);
            sendResponse(requestUs, requestThem, 400, "IOException", uid, EMPTY_BODY);
            return;
        } catch (NumberFormatException e) {
            sendResponse(requestUs, requestThem, 400, "Range is not numeric", uid, EMPTY_BODY);
            return;
        } catch (Exception e) {
            sendResponse(requestUs, requestThem, 500, "Unknown error", uid, EMPTY_BODY);
            return;
        }
        byte[] body = byteBuffer.toByteArray();
        // debug("Sent sha1 is " + sha1sum(body));
        sendResponse(requestUs, requestThem, 200, "OK", uid, body);
    } else {
        debug("Unknown method / url " + requestMethod + " " + url);
        sendResponse(requestUs, requestThem, 400, "OK", uid, EMPTY_BODY);
    }
}
Also used : HttpException(cz.msebera.android.httpclient.HttpException) HttpRequest(cz.msebera.android.httpclient.HttpRequest) BasicHttpRequest(cz.msebera.android.httpclient.message.BasicHttpRequest) AbstractSessionInputBuffer(cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer) SessionInputBuffer(cz.msebera.android.httpclient.io.SessionInputBuffer) HttpRequestParser(cz.msebera.android.httpclient.impl.io.HttpRequestParser) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(info.guardianproject.iocipher.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemoteException(android.os.RemoteException) HttpException(cz.msebera.android.httpclient.HttpException) IOException(java.io.IOException) MethodNotSupportedException(cz.msebera.android.httpclient.MethodNotSupportedException) Header(cz.msebera.android.httpclient.Header) RemoteException(android.os.RemoteException) RandomAccessFile(info.guardianproject.iocipher.RandomAccessFile) File(info.guardianproject.iocipher.File)

Example 9 with File

use of info.guardianproject.iocipher.File in project Zom-Android by zom.

the class SecureMediaStore method getThumbnailVfs.

public static Bitmap getThumbnailVfs(Uri uri, int thumbnailSize) {
    if (!VirtualFileSystem.get().isMounted())
        return null;
    File image = new File(uri.getPath());
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inInputShareable = true;
    options.inPurgeable = true;
    try {
        FileInputStream fis = new FileInputStream(new File(image.getPath()));
        BitmapFactory.decodeStream(fis, null, options);
    } catch (Exception e) {
        LogCleaner.warn(ImApp.LOG_TAG, "unable to read vfs thumbnail" + e.toString());
        return null;
    }
    if ((options.outWidth == -1) || (options.outHeight == -1))
        return null;
    int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / thumbnailSize;
    try {
        FileInputStream fis = new FileInputStream(new File(image.getPath()));
        Bitmap scaledBitmap = BitmapFactory.decodeStream(fis, null, opts);
        return scaledBitmap;
    } catch (FileNotFoundException e) {
        LogCleaner.warn(ImApp.LOG_TAG, "can't find IOcipher file: " + image.getPath());
        return null;
    } catch (OutOfMemoryError oe) {
        LogCleaner.error(ImApp.LOG_TAG, "out of memory loading thumbnail: " + image.getPath(), oe);
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileNotFoundException(java.io.FileNotFoundException) BitmapFactory(android.graphics.BitmapFactory) File(info.guardianproject.iocipher.File) FileInputStream(info.guardianproject.iocipher.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 10 with File

use of info.guardianproject.iocipher.File in project Zom-Android by zom.

the class SecureMediaStore method copyToVfs.

public static void copyToVfs(java.io.File sourceFile, String targetPath) throws IOException {
    // create the target directories tree
    mkdirs(targetPath);
    // copy
    java.io.InputStream fis = null;
    fis = new java.io.FileInputStream(sourceFile);
    FileOutputStream fos = new FileOutputStream(new File(targetPath), false);
    IOUtils.copyLarge(fis, fos);
    fos.close();
    fis.close();
}
Also used : FileOutputStream(info.guardianproject.iocipher.FileOutputStream) File(info.guardianproject.iocipher.File) InputStream(java.io.InputStream)

Aggregations

File (info.guardianproject.iocipher.File)32 IOException (java.io.IOException)14 FileOutputStream (info.guardianproject.iocipher.FileOutputStream)6 FileInputStream (info.guardianproject.iocipher.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 Bitmap (android.graphics.Bitmap)4 Uri (android.net.Uri)4 HashMap (java.util.HashMap)4 RemoteException (android.os.RemoteException)3 RandomAccessFile (info.guardianproject.iocipher.RandomAccessFile)3 BitmapFactory (android.graphics.BitmapFactory)2 MediaDataSource (android.media.MediaDataSource)2 MediaPlayer (android.media.MediaPlayer)2 HttpRequest (cz.msebera.android.httpclient.HttpRequest)2 BasicHttpRequest (cz.msebera.android.httpclient.message.BasicHttpRequest)2 InputStream (java.io.InputStream)2 HttpMediaStreamer (org.awesomeapp.messenger.util.HttpMediaStreamer)2 OmemoFingerprint (org.jivesoftware.smackx.omemo.OmemoFingerprint)2 OmemoDevice (org.jivesoftware.smackx.omemo.internal.OmemoDevice)2 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)2