Esempio n. 1
0
  /**
   * Get the Mime type of an Asset based on its type. If the Asset already has the "content-type"
   * property set, we return that. Otherwise the Apache Tika library is used to do file type
   * detection.
   *
   * @return A string representation of the content type suitable for use in an HTTP header. Eg.
   *     "image/jpeg" for a jpeg image.
   */
  public <T> String getMimeType(Entity entity, T type) {

    Map<String, Object> fileMetadata = AssetUtils.getFileMetadata(entity);
    if (fileMetadata.get(AssetUtils.CONTENT_TYPE) != null) {
      return (String) fileMetadata.get(AssetUtils.CONTENT_TYPE);
    }

    Metadata metadata = new Metadata();
    MediaType mediaType = MediaType.OCTET_STREAM;
    try {
      if (type instanceof byte[]) {

        ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) type);
        mediaType = detector.detect(bais, metadata);
      } else if (type instanceof File) {

        InputStream fis = new BufferedInputStream(new FileInputStream((File) type));
        try {
          mediaType = detector.detect(fis, metadata);
        } finally {
          fis.close();
        }
      } else {
        return mediaType.toString();
      }

      fileMetadata.put(AssetUtils.CONTENT_TYPE, mediaType.toString());
    } catch (IOException e) {
      LOG.error("error detecting mime type", e);
    }

    return mediaType.toString();
  }
Esempio n. 2
0
  // Upgrading database
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    this.db = db;
    Log.i(Constants.TAG, "Upgrading database version from " + oldVersion + " to " + newVersion);

    UpgradeProcessor.process(oldVersion, newVersion);

    try {
      for (String sqlFile : AssetUtils.list(SQL_DIR, mContext.getAssets())) {
        if (sqlFile.startsWith(UPGRADE_QUERY_PREFIX)) {
          int fileVersion =
              Integer.parseInt(
                  sqlFile.substring(
                      UPGRADE_QUERY_PREFIX.length(),
                      sqlFile.length() - UPGRADE_QUERY_SUFFIX.length()));
          if (fileVersion > oldVersion && fileVersion <= newVersion) {
            execSqlFile(sqlFile, db);
          }
        }
      }
      Log.i(Constants.TAG, "Database upgrade successful");

    } catch (IOException e) {
      throw new RuntimeException("Database upgrade failed", e);
    }
  }