Exemplo n.º 1
0
  private OCFile obtainNewOCFileToUpload(
      String remotePath, String localPath, String mimeType, FileDataStorageManager storageManager) {
    OCFile newFile = new OCFile(remotePath);
    newFile.setStoragePath(localPath);
    newFile.setLastSyncDateForProperties(0);
    newFile.setLastSyncDateForData(0);

    // size
    if (localPath != null && localPath.length() > 0) {
      File localFile = new File(localPath);
      newFile.setFileLength(localFile.length());
      newFile.setLastSyncDateForData(localFile.lastModified());
    } // don't worry about not assigning size, the problems with localPath
    // are checked when the UploadFileOperation instance is created

    // MIME type
    if (mimeType == null || mimeType.length() <= 0) {
      try {
        mimeType =
            MimeTypeMap.getSingleton()
                .getMimeTypeFromExtension(remotePath.substring(remotePath.lastIndexOf('.') + 1));
      } catch (IndexOutOfBoundsException e) {
        Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + remotePath);
      }
    }
    if (mimeType == null) {
      mimeType = "application/octet-stream";
    }
    newFile.setMimetype(mimeType);

    return newFile;
  }
Exemplo n.º 2
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.º 3
0
 @Override
 public void onNeutral(String callerTag) {
   File f = null;
   if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {
     f.delete();
     mFile.setStoragePath(null);
     mStorageManager.saveFile(mFile);
     updateFileDetails(mFile, mAccount);
   }
 }
Exemplo n.º 4
0
  public void onDismiss(EditNameFragment dialog) {
    if (dialog instanceof EditNameFragment) {
      if (((EditNameFragment) dialog).getResult()) {
        String newFilename = ((EditNameFragment) dialog).getNewFilename();
        Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
        if (!newFilename.equals(mFile.getFileName())) {
          FileDataStorageManager fdsm =
              new FileDataStorageManager(mAccount, getActivity().getContentResolver());
          if (fdsm.getFileById(mFile.getFileId()) != null) {
            OCFile newFile =
                new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + newFilename);
            newFile.setCreationTimestamp(mFile.getCreationTimestamp());
            newFile.setFileId(mFile.getFileId());
            newFile.setFileLength(mFile.getFileLength());
            newFile.setKeepInSync(mFile.keepInSync());
            newFile.setLastSyncDate(mFile.getLastSyncDate());
            newFile.setMimetype(mFile.getMimetype());
            newFile.setModificationTimestamp(mFile.getModificationTimestamp());
            newFile.setParentId(mFile.getParentId());
            boolean localRenameFails = false;
            if (mFile.isDown()) {
              File f = new File(mFile.getStoragePath());
              Log.e(TAG, f.getAbsolutePath());
              localRenameFails =
                  !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));
              Log.e(TAG, f.getParent() + File.separator + newFilename);
              newFile.setStoragePath(f.getParent() + File.separator + newFilename);
            }

            if (localRenameFails) {
              Toast msg =
                  Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
              msg.show();

            } else {
              new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
              boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
              getActivity()
                  .showDialog(
                      (inDisplayActivity)
                          ? FileDisplayActivity.DIALOG_SHORT_WAIT
                          : FileDetailActivity.DIALOG_SHORT_WAIT);
            }
          }
        }
      }
    } else {
      Log.e(
          TAG,
          "Unknown dialog instance passed to onDismissDalog: "
              + dialog.getClass().getCanonicalName());
    }
  }
Exemplo n.º 5
0
 @Override
 public void onNeutral(String callerTag) {
   FileDataStorageManager fdsm =
       new FileDataStorageManager(mAccount, getActivity().getContentResolver());
   File f = null;
   if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {
     f.delete();
     mFile.setStoragePath(null);
     fdsm.saveFile(mFile);
     updateFileDetails(mFile, mAccount);
   }
 }
Exemplo n.º 6
0
  private void saveLocalFile() {
    mFile.setFileName(mNewName);

    // try to rename the local copy of the file
    if (mFile.isDown()) {
      File f = new File(mFile.getStoragePath());
      String parentStoragePath = f.getParent();
      if (!parentStoragePath.endsWith(File.separator)) parentStoragePath += File.separator;
      if (f.renameTo(new File(parentStoragePath + mNewName))) {
        mFile.setStoragePath(parentStoragePath + mNewName);
      }
      // else - NOTHING: the link to the local file is kept although the local name can't be updated
      // TODO - study conditions when this could be a problem
    }

    mStorageManager.saveFile(mFile);
  }
    /**
     * Performs the movement
     *
     * @return 'False' when the movement of any file fails.
     */
    @Override
    protected Boolean doInBackground(Void... params) {
      while (!mLocalPaths.isEmpty()) {
        String currentPath = mLocalPaths.get(0);
        File currentFile = new File(currentPath);
        String expectedPath = FileStorageUtils.getSavePath(mAccount.name) + mRemotePaths.get(0);
        File expectedFile = new File(expectedPath);

        if (expectedFile.equals(currentFile) || currentFile.renameTo(expectedFile)) {
          // SUCCESS
          OCFile file = mStorageManager.getFileByPath(mRemotePaths.get(0));
          file.setStoragePath(expectedPath);
          mStorageManager.saveFile(file);
          mRemotePaths.remove(0);
          mLocalPaths.remove(0);

        } else {
          // FAIL
          return false;
        }
      }
      return true;
    }