public static String getSize(AttachmentProxy a, ReadableDatabase database) {
    if (a.getFile() != null) {
      int size = (int) a.getFile().length();
      return getSizeInKb(size);
    }

    AttachmentPacket packet = a.getPendingPacket();
    if (packet != null) {
      return getSizeInKb(packet.getFileSize());
    }

    int size = 0;
    UniversalId id = a.getUniversalId();
    try {
      DatabaseKey key = DatabaseKey.createMutableKey(id);
      if (!database.doesRecordExist(key)) key = DatabaseKey.createImmutableKey(id);
      if (!database.doesRecordExist(key)) return "";
      size = database.getRecordSize(key);
    } catch (Exception e) {
      e.printStackTrace();
    }

    size -= 1024; // Public code & overhead
    size = size * 3 / 4; // Base64 overhead
    return getSizeInKb(size);
  }
Exemple #2
0
  public void addPrivateAttachment(AttachmentProxy a)
      throws IOException, MartusCrypto.EncryptionException {
    BulletinHeaderPacket bhp = getBulletinHeaderPacket();
    File rawFile = a.getFile();
    if (rawFile != null) {
      SessionKey sessionKeyBytes = getSignatureGenerator().createSessionKey();
      AttachmentPacket ap =
          new AttachmentPacket(getAccount(), sessionKeyBytes, rawFile, getSignatureGenerator());
      bhp.addPrivateAttachmentLocalId(ap.getLocalId());
      a.setPendingPacket(ap, sessionKeyBytes);
    } else {
      bhp.addPrivateAttachmentLocalId(a.getUniversalId().getLocalId());
    }

    getPrivateFieldDataPacket().addAttachment(a);
  }