AutoUploadJob.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2017 Mario Danic
  6. * Copyright (C) 2016 Tobias Kaminsky
  7. * Copyright (C) 2016 Nextcloud
  8. * <p>
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. * <p>
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. * <p>
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.jobs;
  23. import android.accounts.Account;
  24. import android.content.Context;
  25. import android.support.annotation.NonNull;
  26. import com.evernote.android.job.Job;
  27. import com.evernote.android.job.util.support.PersistableBundleCompat;
  28. import com.owncloud.android.MainApp;
  29. import com.owncloud.android.authentication.AccountUtils;
  30. import com.owncloud.android.files.services.FileUploader;
  31. import com.owncloud.android.operations.UploadFileOperation;
  32. import com.owncloud.android.utils.MimeTypeUtil;
  33. import java.io.File;
  34. public class AutoUploadJob extends Job {
  35. public static final String TAG = "AutoUploadJob";
  36. public static final String LOCAL_PATH = "filePath";
  37. public static final String REMOTE_PATH = "remotePath";
  38. public static final String ACCOUNT = "account";
  39. public static final String UPLOAD_BEHAVIOUR = "uploadBehaviour";
  40. @NonNull
  41. @Override
  42. protected Result onRunJob(Params params) {
  43. final Context context = MainApp.getAppContext();
  44. PersistableBundleCompat bundle = params.getExtras();
  45. final String filePath = bundle.getString(LOCAL_PATH, "");
  46. final String remotePath = bundle.getString(REMOTE_PATH, "");
  47. final Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT, ""));
  48. final Integer uploadBehaviour = bundle.getInt(UPLOAD_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_FORGET);
  49. File file = new File(filePath);
  50. // File can be deleted between job generation and job execution. If file does not exist, just ignore it
  51. if (file.exists()) {
  52. final String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
  53. final FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  54. requester.uploadNewFile(
  55. context,
  56. account,
  57. filePath,
  58. remotePath,
  59. uploadBehaviour,
  60. mimeType,
  61. true, // create parent folder if not existent
  62. UploadFileOperation.CREATED_AS_INSTANT_PICTURE
  63. );
  64. }
  65. return Result.SUCCESS;
  66. }
  67. }