internal async Task <WsFile> UploadFile(Stream sourceStream, long sourceSize, WsFilePath targetFilePath, bool ensureDeleteIfNeed, CancellationToken cancellationToken, IProgress <int> progress) { if (sourceStream == null) { throw new ArgumentNullException(nameof(sourceStream)); } if (sourceStream.CanRead == false) { throw new NotSupportedException("The source stream does not support reading."); } if (cancellationToken == null) { throw new ArgumentNullException(nameof(cancellationToken)); } CheckConnected(); progress?.Report(0); if (ensureDeleteIfNeed) { WsFile file = await FindFile(targetFilePath); if (file != null) { await file.Delete(); } } MultipartFormDataContent form = new MultipartFormDataContent(); form.Add(new StringContent(WebUtility.UrlEncode(targetFilePath.Name)), "name"); form.Add(new StringContent(_loginInfo.LoginToken), "wst"); form.Add(new StringContent(WebUtility.UrlEncode(targetFilePath.Folder.FullPath)), "folder"); form.Add(new StringContent(targetFilePath.IsPrivate ? "1" : "0"), "private"); form.Add(new StringContent("0"), "adult"); form.Add(new StringContent(sourceSize.ToString()), "total"); //form.Add(new StringContent(""), "offset"); TransportStream sourceTransportStream = new TransportStream(sourceStream, sourceSize, progress); TransportStreamContent fileContent = new TransportStreamContent(sourceTransportStream, targetFilePath.Name, CalculateBuffer(sourceSize)); form.Add(fileContent); UploadResult uploadResult = await _httpClient.PostFormData <UploadResult>(await GetUploadFileUrl(), form, cancellationToken, false, TimeSpan.FromHours(24)); if (string.IsNullOrWhiteSpace(uploadResult?.Ident)) { throw new IOException($"Server upload error: result={uploadResult.Result}"); } return(_createdFileResolver.Add(targetFilePath.Folder, targetFilePath.Name, uploadResult.Ident, sourceSize)); }
internal async Task MoveItem(WsItem sourceItem, WsFolder targetFolder) { if (sourceItem == null) { throw new ArgumentNullException(nameof(sourceItem)); } if (targetFolder == null) { throw new ArgumentNullException(nameof(targetFolder)); } if (sourceItem.PathInfoGeneric.Folder.Equals(targetFolder.PathInfo)) { throw new ArgumentException($"{nameof(sourceItem)} and {nameof(targetFolder)} are equal folder."); } CheckConnected(); Task <Result> MoveFunc() { FormUrlEncodedContent formContent = CreateFormContent(new[] { new KeyValuePair <string, string>("src", sourceItem.PathInfoGeneric.FullPath), new KeyValuePair <string, string>("src_private", sourceItem.PathInfoGeneric.IsPrivate ? "1" : "0"), new KeyValuePair <string, string>("dest", targetFolder.PathInfo.FullPath), new KeyValuePair <string, string>("dest_private", targetFolder.PathInfo.IsPrivate ? "1" : "0"), }); return(_httpClient.PostFormData <Result>(API_MOVE_FILE_URI, formContent)); } if (sourceItem is WsFile file) { WsFile targetFile = await FindFile(new WsFilePath(targetFolder.PathInfo, file.PathInfo.Name)); if (targetFile != null) { await targetFile.Delete(); } await PostFormDataWithNotFoundAndLoginRetry(file, MoveFunc); } else { await PostFormDataWithLoginRetry(MoveFunc); } _createdFileResolver.MoveItem(sourceItem, targetFolder); sourceItem.ChangePathInInstance(targetFolder.PathInfo.FullPath + sourceItem.PathInfoGeneric.Name, targetFolder.PathInfo.IsPrivate); }
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); } }
internal async Task RenameItem(WsItem item, string newName) { if (string.IsNullOrWhiteSpace(newName)) { throw new ArgumentNullException(nameof(newName)); } CheckConnected(); Task <Result> RenameFunc() { FormUrlEncodedContent formContent = CreateFormContent(new[] { new KeyValuePair <string, string>("ident", item.Ident), new KeyValuePair <string, string>("name", newName) }); return(_httpClient.PostFormData <Result>(API_RENAME_FILE_URI, formContent)); } if (item is WsFile file) { WsFile targetFile = await FindFile(new WsFilePath(file.PathInfo.Folder, newName)); if (targetFile != null) { await targetFile.Delete(); } await PostFormDataWithNotFoundAndLoginRetry(file, RenameFunc); } else { await PostFormDataWithLoginRetry(RenameFunc); } _createdFileResolver.RenameItem(item, newName); item.ChangeNameInInstance(newName); }