public async Task <FileSystemExitCode> UploadFile(FileInfo sourceFileName, WsPath targetFileName, bool overwrite, IProgress <int> progress, CancellationToken cancellationToken)
        {
            try
            {
                await EnsureLogin();

                bool       uploadDirect   = overwrite;
                WsFilePath targetFilePath = targetFileName.GetFilePath();
                if (overwrite == false)
                {
                    WsFile file = await _apiClient.FindFile(targetFilePath);

                    if (file != null)
                    {
                        if (overwrite == false)
                        {
                            return(FileSystemExitCode.FileExists);
                        }
                        using (FileStream sourceStream = sourceFileName.OpenRead())
                        {
                            await file.Replace(sourceStream, cancellationToken, progress);
                        }
                    }
                    else
                    {
                        uploadDirect = true;
                    }
                }
                if (uploadDirect)
                {
                    using (FileStream sourceStream = sourceFileName.OpenRead())
                    {
                        await _apiClient.UploadFile(sourceStream, sourceStream.Length, targetFilePath, cancellationToken, progress);
                    }
                }
                return(FileSystemExitCode.OK);
            }
            catch (TaskCanceledException)
            {
                return(FileSystemExitCode.UserAbort);
            }
            catch (FileNotFoundException)
            {
                return(FileSystemExitCode.FileNotFound);
            }
            // TODO: dialog for file password, Exception throw in WsApiClient.CheckResultStatus
            //catch (??)
            //{
            //    return ??;
            //}
            catch (Exception ex)
            {
                if (ShowError("Upload file error", ex, true) == false)
                {
                    return(FileSystemExitCode.UserAbort);
                }
                return(FileSystemExitCode.ReadError);
            }
        }
        public FileSystemExitCode CopyFile(WsPath sourceFilePath, WsPath targetPath, bool overwrite, IProgress <int> progress)
        {
            try
            {
                using (ThreadKeeper exec = new ThreadKeeper())
                {
                    return(exec.ExecAsync(async(cancellationToken) =>
                    {
                        await EnsureLogin();
                        WsFile sourceFile = await _apiClient.FindFile(sourceFilePath.GetFilePath());
                        if (sourceFile != null)
                        {
                            if (overwrite == false)
                            {
                                if (await _apiClient.FindFile(targetPath.GetFilePath()) != null)
                                {
                                    return FileSystemExitCode.FileExists;
                                }
                            }
                        }

                        if (sourceFile == null)
                        {
                            return FileSystemExitCode.FileNotFound;
                        }
                        WsFolder targetFolder = await _apiClient.FindFolder(targetPath.Parent.GetFolderPath());
                        if (targetFolder == null)
                        {
                            return FileSystemExitCode.FileNotFound;
                        }

                        await sourceFile.Copy(targetFolder, cancellationToken, new ThreadKeeperCancellableProgress(exec, progress));
                        return FileSystemExitCode.OK;
                    }));
                }
            }
            catch (TaskCanceledException)
            {
                return(FileSystemExitCode.UserAbort);
            }
            catch (Exception ex)
            {
                ShowError("Copy file error", ex, false);
                return(FileSystemExitCode.WriteError);
            }
        }
 public PreviewBitmapResult GetPreviewBitmap(WsPath filePath, int width, int height)
 {
     try
     {
         return(ExecuteAsync(filePath, async() =>
         {
             WsFile file = await _apiClient.FindFile(filePath.GetFilePath());
             if (file != null)
             {
                 WsFolder folder = await _apiClient.FindFolder(filePath.Parent.GetFolderPath());
                 if (folder != null)
                 {
                     WsFilePreview filePreview = await _filesPreviewCache.FindFilePreview(folder, filePath.Name);
                     byte[] jpgData = await filePreview.JpgData;
                     if (jpgData?.Length > 0)
                     {
                         using (MemoryStream jpgStream = new MemoryStream(jpgData))
                         {
                             Image jpg = Image.FromStream(jpgStream);
                             decimal ratio = (decimal)jpg.Width / jpg.Height;
                             if (ratio > 1)
                             {
                                 height = (int)(height / ratio);
                             }
                             if (ratio < 1)
                             {
                                 width = (int)(width / ratio);
                             }
                             Bitmap bmp = new Bitmap(jpg, width, height);
                             return PreviewBitmapResult.Extracted(bmp, null, false);
                         }
                     }
                 }
             }
             return PreviewBitmapResult.None;
         }));
     }
     catch
     {
         return(PreviewBitmapResult.None);
     }
 }
 public bool DeleteFile(WsPath filePath)
 {
     try
     {
         return(ExecuteAsync(filePath, async() =>
         {
             WsFile file = await _apiClient.FindFile(filePath.GetFilePath());
             if (file == null)
             {
                 return false;
             }
             await file.Delete();
             return true;
         }));
     }
     catch (Exception ex)
     {
         ShowError("Delete file error", ex, false);
         return(false);
     }
 }
        public async Task <FileSystemExitCode> DownloadFile(WsPath sourceFileName, FileInfo targetFileName, bool overwrite, IProgress <int> progress, bool deleteAfter, CancellationToken cancellationToken)
        {
            try
            {
                await EnsureLogin();

                WsFile file = await _apiClient.GetFile(sourceFileName.GetFilePath());

                FileMode mode = overwrite ? FileMode.Create : FileMode.CreateNew;

                using (FileStream targetStream = targetFileName.Open(mode, FileAccess.Write))
                {
                    await file.Download(targetStream, cancellationToken, progress);
                }
                targetFileName.CreationTime = targetFileName.LastWriteTime = file.Created;
                if (deleteAfter)
                {
                    await file.Delete();
                }
                return(FileSystemExitCode.OK);
            }
            catch (TaskCanceledException)
            {
                targetFileName.Delete();
                return(FileSystemExitCode.UserAbort);
            }
            catch (FileNotFoundException)
            {
                return(FileSystemExitCode.FileNotFound);
            }
            catch (Exception ex)
            {
                targetFileName.Delete();
                if (ShowError("Download file error", ex, true) == false)
                {
                    return(FileSystemExitCode.UserAbort);
                }
                return(FileSystemExitCode.ReadError);
            }
        }
        public FileSystemExitCode MoveOrRenameItem(WsPath sourcePath, WsPath targetPath, bool overwrite, bool sourceIsFolder)
        {
            try
            {
                return(ExecuteAsync(sourcePath, async() =>
                {
                    WsItem sourceItem;
                    if (sourceIsFolder)
                    {
                        sourceItem = await _apiClient.FindFolder(sourcePath.GetFolderPath());
                        if (sourceItem != null)
                        {
                            if (overwrite == false)
                            {
                                if (await _apiClient.FindFolder(targetPath.GetFolderPath()) != null)
                                {
                                    return FileSystemExitCode.FileExists; // TODO: not work for renaming to existing folder
                                }
                            }
                        }
                    }
                    else
                    {
                        sourceItem = await _apiClient.FindFile(sourcePath.GetFilePath());
                        if (sourceItem != null)
                        {
                            if (overwrite == false)
                            {
                                if (await _apiClient.FindFile(targetPath.GetFilePath()) != null)
                                {
                                    return FileSystemExitCode.FileExists; // TODO: not work for renaming to existing file
                                }
                            }
                        }
                    }
                    if (sourceItem == null)
                    {
                        return FileSystemExitCode.FileNotFound;
                    }

                    if (sourcePath.Parent.Path == targetPath.Parent.Path)
                    {
                        await sourceItem.Rename(targetPath.Name);
                    }
                    else
                    {
                        WsFolder targetFolder = await _apiClient.FindFolder(targetPath.Parent.GetFolderPath());
                        if (targetFolder == null)
                        {
                            return FileSystemExitCode.FileNotFound;
                        }
                        await sourceItem.Move(targetFolder);
                    }
                    return FileSystemExitCode.OK;
                }));
            }
            catch (Exception ex)
            {
                ShowError("Move/rename file/folder error", ex, false);
                return(FileSystemExitCode.WriteError);
            }
        }