Exemplo n.º 1
0
  /**
   * Saves a OC File after a successful upload.
   *
   * <p>A PROPFIND is necessary to keep the props in the local database synchronized with the
   * server, specially the modification time and Etag (where available)
   *
   * <p>TODO refactor this ugly thing
   */
  private void saveUploadedFile() {
    OCFile file = mCurrentUpload.getFile();
    if (file.fileExists()) {
      file = mStorageManager.getFileById(file.getFileId());
    }
    long syncDate = System.currentTimeMillis();
    file.setLastSyncDateForData(syncDate);

    // new PROPFIND to keep data consistent with server
    // in theory, should return the same we already have
    ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mCurrentUpload.getRemotePath());
    RemoteOperationResult result = operation.execute(mUploadClient);
    if (result.isSuccess()) {
      updateOCFile(file, (RemoteFile) result.getData().get(0));
      file.setLastSyncDateForProperties(syncDate);
    }

    // / maybe this would be better as part of UploadFileOperation... or
    // maybe all this method
    if (mCurrentUpload.wasRenamed()) {
      OCFile oldFile = mCurrentUpload.getOldFile();
      if (oldFile.fileExists()) {
        oldFile.setStoragePath(null);
        mStorageManager.saveFile(oldFile);
      } // else: it was just an automatic renaming due to a name
      // coincidence; nothing else is needed, the storagePath is right
      // in the instance returned by mCurrentUpload.getFile()
    }

    mStorageManager.saveFile(file);
  }
Exemplo n.º 2
0
 /**
  * Sends a broadcast in order to the interested activities can update their view
  *
  * @param upload Finished upload operation
  * @param uploadResult Result of the upload operation
  */
 private void sendFinalBroadcast(UploadFileOperation upload, RemoteOperationResult uploadResult) {
   Intent end = new Intent(getUploadFinishMessage());
   end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath()); // real remote
   // path, after
   // possible
   // automatic
   // renaming
   if (upload.wasRenamed()) {
     end.putExtra(EXTRA_OLD_REMOTE_PATH, upload.getOldFile().getRemotePath());
   }
   end.putExtra(EXTRA_OLD_FILE_PATH, upload.getOriginalStoragePath());
   end.putExtra(ACCOUNT_NAME, upload.getAccount().name);
   end.putExtra(EXTRA_UPLOAD_RESULT, uploadResult.isSuccess());
   sendStickyBroadcast(end);
 }
Exemplo n.º 3
0
  /**
   * Core upload method: sends the file(s) to upload
   *
   * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
   */
  public void uploadFile(String uploadKey) {

    synchronized (mPendingUploads) {
      mCurrentUpload = mPendingUploads.get(uploadKey);
    }

    if (mCurrentUpload != null) {

      notifyUploadStart(mCurrentUpload);

      RemoteOperationResult uploadResult = null, grantResult = null;

      try {
        /// prepare client object to send requests to the ownCloud server
        if (mUploadClient == null || !mLastAccount.equals(mCurrentUpload.getAccount())) {
          mLastAccount = mCurrentUpload.getAccount();
          mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
          mUploadClient =
              OwnCloudClientFactory.createOwnCloudClient(mLastAccount, getApplicationContext());
        }

        /// check the existence of the parent folder for the file to upload
        String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
        remoteParentPath =
            remoteParentPath.endsWith(OCFile.PATH_SEPARATOR)
                ? remoteParentPath
                : remoteParentPath + OCFile.PATH_SEPARATOR;
        grantResult = grantFolderExistence(remoteParentPath);

        /// perform the upload
        if (grantResult.isSuccess()) {
          OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
          mCurrentUpload.getFile().setParentId(parent.getFileId());
          uploadResult = mCurrentUpload.execute(mUploadClient);
          if (uploadResult.isSuccess()) {
            saveUploadedFile();
          }
        } else {
          uploadResult = grantResult;
        }

      } catch (AccountsException e) {
        Log_OC.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
        uploadResult = new RemoteOperationResult(e);

      } catch (IOException e) {
        Log_OC.e(TAG, "Error while trying to get autorization for " + mLastAccount.name, e);
        uploadResult = new RemoteOperationResult(e);

      } finally {
        synchronized (mPendingUploads) {
          mPendingUploads.remove(uploadKey);
          Log_OC.i(TAG, "Remove CurrentUploadItem from pending upload Item Map.");
        }
        if (uploadResult.isException()) {
          // enforce the creation of a new client object for next uploads; this grant that a new
          // socket will
          // be created in the future if the current exception is due to an abrupt lose of network
          // connection
          mUploadClient = null;
        }
      }

      /// notify result

      notifyUploadResult(uploadResult, mCurrentUpload);
      sendFinalBroadcast(mCurrentUpload, uploadResult);
    }
  }