예제 #1
0
  @Override
  protected void onHandleIntent(Intent intent) {
    String filePath = intent.getStringExtra(KEY_FILE);
    String url = intent.getStringExtra(KEY_URL);

    if (TextUtils.isEmpty(filePath) && TextUtils.isEmpty(url)) {
      LogUtil.w(TAG, "Nothing passed to upload, bailing");
      return;
    }

    String title = intent.getStringExtra(KEY_TITLE);
    String desc = intent.getStringExtra(KEY_DESC);
    mNotificationId = intent.getIntExtra(KEY_NOTIF_ID, -1);

    if (!TextUtils.isEmpty(filePath)) {
      uploadPhoto(title, desc, filePath);
    } else {
      uploadLink(title, desc, url);
    }
  }
예제 #2
0
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);

    if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED
        || response.code() == HttpURLConnection.HTTP_FORBIDDEN) {
      String token = ApiClient.getAccessToken();

      if (!TextUtils.isEmpty(token)) {
        LogUtil.v(TAG, "Token is no longer valid");

        synchronized (sLock) {
          String currentToken = ApiClient.getAccessToken();

          // Check if our current token has been updated, if it hasn't fetch a new one.
          if (!TextUtils.isEmpty(currentToken) && currentToken.equals(token)) {
            ApiClient.setAccessToken(refreshToken(OpengurApp.getInstance()));
          }
        }

        if (!TextUtils.isEmpty(ApiClient.getAccessToken())) {
          Request newRequest =
              request
                  .newBuilder()
                  .removeHeader(ApiClient.AUTHORIZATION_HEADER)
                  .addHeader(ApiClient.AUTHORIZATION_HEADER, "Bearer " + ApiClient.getAccessToken())
                  .build();

          return chain.proceed(newRequest);
        }
      } else {
        LogUtil.w(TAG, "Received unauthorized status from API but no access token present... wat?");
      }
    }

    return response;
  }
예제 #3
0
  private void uploadPhoto(
      @Nullable String title, @Nullable String description, @NonNull String filePath) {
    final File file = new File(filePath);

    if (!FileUtil.isFileValid(file)) {
      LogUtil.w(TAG, "File is invalid, bailing");
      return;
    }

    LogUtil.v(TAG, "Received File to upload");
    ApiClient client = new ApiClient(Endpoints.UPLOAD.getUrl(), ApiClient.HttpRequest.POST);
    MediaType type;

    if (file.getAbsolutePath().endsWith("png")) {
      type = MediaType.parse("image/png");
    } else if (file.getAbsolutePath().endsWith("gif")) {
      type = MediaType.parse("image/gif");
    } else {
      type = MediaType.parse("image/jpeg");
    }

    MultipartBuilder builder =
        new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addPart(
                Headers.of("Content-Disposition", "form-data; name=\"image\""),
                new ProgressRequestBody(file, type, this));

    builder.addPart(
        Headers.of("Content-Disposition", "form-data; name=\"type\""),
        RequestBody.create(null, "file"));

    if (!TextUtils.isEmpty(title)) {
      builder.addPart(
          Headers.of("Content-Disposition", "form-data; name=\"title\""),
          RequestBody.create(null, title));
    }

    if (!TextUtils.isEmpty(description)) {
      builder.addPart(
          Headers.of("Content-Disposition", "form-data; name=\"description\""),
          RequestBody.create(null, description));
    }

    RequestBody body = builder.build();

    try {
      if (mNotificationId <= 0) mNotificationId = file.hashCode();
      onUploadStarted();
      JSONObject json = client.doWork(body);
      int status = json.getInt(ApiClient.KEY_STATUS);

      if (status == ApiClient.STATUS_OK) {
        handleResponse(json.getJSONObject(ApiClient.KEY_DATA));
      } else {
        onUploadFailed(title, description, filePath, true);
      }
    } catch (IOException | JSONException e) {
      LogUtil.e(TAG, "Error uploading file", e);
      onUploadFailed(title, description, filePath, true);
    }
  }