/// <summary> /// Get a thumbnail for the given file /// </summary> /// <param name="dirEntry">The directory entry of the file</param> /// <param name="size">The size of the thumbnail (vertical and horizontal pixel count)</param> /// <returns></returns> public async Task <byte[]> GetThumbnailImage(SeafDirEntry dirEntry, int size) { dirEntry.ThrowOnNull(nameof(dirEntry)); if (dirEntry.Type != DirEntryType.File) { throw new ArgumentException("The given directory entry is not a file."); } return(await GetThumbnailImage(dirEntry.LibraryId, dirEntry.Path, size)); }
/// <summary> /// Move the given file /// </summary> /// <param name="dirEntry">The directory entry of the file to move</param> /// <param name="targetLibrary">The library to move this file to</param> /// <param name="targetDirectory">The directory to move this file to</param> /// <returns>A value which indicates if the action was successful</returns> public async Task <bool> MoveFile(SeafDirEntry dirEntry, SeafLibrary targetLibrary, string targetDirectory) { dirEntry.ThrowOnNull(nameof(dirEntry)); if (dirEntry.Type != DirEntryType.File) { throw new ArgumentException("The given directory entry is not a file."); } return(await MoveFile(dirEntry.LibraryId, dirEntry.Path, targetLibrary.Id, targetDirectory)); }
/// <summary> /// Get a download link for the given file /// </summary> /// <param name="dirEntry">The directory entry for the file to download</param> /// <returns>The download link which is valid once</returns> public async Task <string> GetFileDownloadLink(SeafDirEntry dirEntry) { dirEntry.ThrowOnNull(nameof(dirEntry)); if (dirEntry.Type != DirEntryType.File) { throw new ArgumentException("The given directory entry is not a file."); } return(await GetFileDownloadLink(dirEntry.LibraryId, dirEntry.Path)); }
/// <summary> /// Rename the given file /// </summary> /// <param name="dirEntry">The directory entry of the file</param> /// <param name="newName">The new name of the file</param> /// <returns>A value which indicates if the action was successful</returns> public async Task <bool> RenameFile(SeafDirEntry dirEntry, string newName) { dirEntry.ThrowOnNull(nameof(dirEntry)); if (dirEntry.Type != DirEntryType.File) { throw new ArgumentException("The given directory entry is not a file."); } return(await RenameFile(dirEntry.LibraryId, dirEntry.Path, newName)); }
/// <summary> /// Delete the given directory /// </summary> /// <param name="dirEntry">The directory to delete</param> /// <returns>A value which indicates if the action was successful</returns> public async Task <bool> DeleteDirectory(SeafDirEntry dirEntry) { dirEntry.ThrowOnNull(nameof(dirEntry)); if (dirEntry.Type != DirEntryType.Dir) { throw new ArgumentException("The given directory entry is not a directory."); } return(await DeleteDirectory(dirEntry.LibraryId, dirEntry.Path)); }
/// <summary> /// Update the contents of the given, existing file /// </summary> /// <param name="dirEntry">The file to update</param> /// <param name="fileContent">The new content of the file</param> /// <param name="progressCallback">Optional progress callback (will report percentage of upload)</param> public async Task <bool> UpdateSingle(SeafDirEntry dirEntry, Stream fileContent, Action <float> progressCallback = null) { dirEntry.ThrowOnNull(nameof(dirEntry)); fileContent.ThrowOnNull(nameof(fileContent)); if (dirEntry.Type != DirEntryType.File) { throw new ArgumentException("The given dirEntry does not represent a file."); } return(await UpdateSingle(dirEntry.LibraryId, dirEntry.Directory, dirEntry.Name, fileContent, progressCallback)); }
private async void btnGenerateLink_Click(object sender, EventArgs e) { tokenSource = new CancellationTokenSource(); this.btnGenerateLink.Enabled = false; string strPassword = string.Empty; string strExpire = string.Empty; if (cbPassword.Checked) { if (string.IsNullOrEmpty(this.tbPassword.Text)) { MessageBox.Show( Properties.Resources.EmptyPassword, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } if (this.tbPassword.Text.Length < 8) { MessageBox.Show( Properties.Resources.PasswordTooShort, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } if (this.tbPassword.Text != this.tbPasswordR.Text) { MessageBox.Show( Properties.Resources.PasswordNotMatch, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnGenerateLink.Enabled = true; return; } strPassword = this.tbPassword.Text; } if (cbExpire.Checked) { if (string.IsNullOrEmpty(this.tbExpireDay.Text)) { return; } strExpire = this.tbExpireDay.Text; } this.UseWaitCursor = true; try { //var resultGetFileDetail = await _session.GetFileDetail(_libraryID, _path, tokenSource.Token); //if (resultGetFileDetail != null) //{ //var resultCreatShareLink = await _session.CreatShareLink(resultGetFileDetail, tokenSource.Token, strPassword, strExpire); SeafDirEntry resultGetFileDetail = new SeafDirEntry(); resultGetFileDetail.LibraryId = _libraryID; resultGetFileDetail.Path = _path; var resultCreatShareLink = await _session.CreatShareLink(resultGetFileDetail, tokenSource.Token, strPassword, strExpire); if (!string.IsNullOrEmpty(resultCreatShareLink)) { this.DialogResult = DialogResult.OK; JObject o2 = (JObject)JToken.Parse(resultCreatShareLink); this.ShareLink = (string)o2["link"]; if (cbPassword.Checked) { this.ShareLink += string.Format("\r\n {0}:{1}", Properties.Resources.SharePassword, this.tbPassword.Text); } } //} } catch (System.OperationCanceledException ex) { log.Info(ex.Message); } catch (SeafException se) { log.Error(se.SeafError.GetErrorMessage()); MessageBox.Show( se.SeafError.GetErrorMessage(), Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.btnGenerateLink.Enabled = true; this.UseWaitCursor = false; tokenSource.Dispose(); tokenSource = null; } }
private async void btnUpload_Click(object sender, EventArgs e) { log.Info("btnUpload_Click"); if (!bWork) { this.pbUpload.Value = 0; tokenSource = new CancellationTokenSource(); this.btnCancelUplaod.Enabled = false; bWork = true; string host = string.Empty; string user = string.Empty; string strPassWord = string.Empty; try { if (!File.Exists(Application.UserAppDataPath + "\\seafileaddin.cfg")) { log.Error("seafileaddin.cfg not exits"); MessageBox.Show( Properties.Resources.SetAccount, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); //MessageBox.Show("An error occurred while attempting to show the application." + // "The error is:" + Properties.Resources.SetAccount); this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); return; } using (var file = File.OpenText(Application.UserAppDataPath + "\\seafileaddin.cfg")) { using (JsonTextReader reader = new JsonTextReader(file)) { JObject o2 = (JObject)JToken.ReadFrom(reader); host = (string)o2["accessurl"]; user = (string)o2["account"]; strPassWord = DataProtectionExtensions.Unprotect((string)o2["poassword"]); } } } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show( Properties.Resources.ConfigParseError, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); this.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.Close(); // return; } if (string.IsNullOrEmpty(tbFile.Text)) { MessageBox.Show( Properties.Resources.EmptyUploadFile, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } string strPassword = string.Empty; string strExpire = string.Empty; if (cbPassword.Checked) { if (string.IsNullOrEmpty(this.tbPassword.Text)) { MessageBox.Show( Properties.Resources.EmptyPassword, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } if (this.tbPassword.Text.Length < 8) { MessageBox.Show( Properties.Resources.PasswordTooShort, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } if (this.tbPassword.Text != this.tbPasswordR.Text) { MessageBox.Show( Properties.Resources.PasswordNotMatch, Properties.Resources.MessageBoxInfoTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnCancelUplaod.Enabled = true; bWork = false; return; } strPassword = this.tbPassword.Text; } if (cbExpire.Checked) { if (string.IsNullOrEmpty(this.tbExpireDay.Text)) { this.btnCancelUplaod.Enabled = true; bWork = false; return; } strExpire = this.tbExpireDay.Text; } System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, errors) => { return(true); }; char[] pChar = strPassWord.ToCharArray(); SecureString pw = new SecureString(); foreach (char c in pChar) { pw.AppendChar(c); } char[] pwBuf = SecureStringUtils.SecureStringToCharArray(pw); pw.Dispose(); try { session = await SeafileSession.Establish(new Uri(host, UriKind.Absolute), user, pwBuf); // try to connect to the seafile server using the given credentials log.Info("connection success."); this.btnCancelUplaod.Enabled = true; this.btnCancelUplaod.Text = Properties.Resources.Cancel; //TODO loading process... var seafDefaultLibrary = await session.GetDefaultLibrary(); try { var exit = await session.ListDirectory(seafDefaultLibrary, "/outlook"); } catch (SeafException exListDirectory) { log.Info("outlook folder doesn't exit."); if (exListDirectory.SeafError.HttpStatusCode == System.Net.HttpStatusCode.Forbidden) { bool successCreate = await session.CreateDirectory(seafDefaultLibrary, "/outlook"); } } //catch (Exception ex) //{ // log.Error(ex.Message); // MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); // return; //} //check if file exist string fileName = System.IO.Path.GetFileName(tbFile.Text); System.IO.FileStream fs = new System.IO.FileStream(tbFile.Text, System.IO.FileMode.Open); pbUpload.Value = 0; Action <float> ProcessAction = new Action <float>(ProcessCallBack); var resultUploadSingle = await session.UploadSingle(seafDefaultLibrary, "/outlook", fileName, fs, tokenSource.Token, ProcessAction); if (resultUploadSingle != null) { JObject o2 = (JObject)JToken.Parse(resultUploadSingle.Substring(1, resultUploadSingle.Length - 2)); string tmpName = (string)o2["name"]; SeafDirEntry tmp = new SeafDirEntry(); tmp.LibraryId = seafDefaultLibrary.Id; tmp.Path = string.Format("/outlook/{0}", tmpName); var resultCreatShareLink = await session.CreatShareLink(tmp, tokenSource.Token, strPassword, strExpire); if (!string.IsNullOrEmpty(resultCreatShareLink)) { this.DialogResult = DialogResult.OK; //this.ShareLink = resultCreatShareLink; JObject oShare = (JObject)JToken.Parse(resultCreatShareLink); this.ShareLink = (string)oShare["link"]; if (cbPassword.Checked) { this.ShareLink += string.Format("\r\n {0}:{1}", Properties.Resources.SharePassword, this.tbPassword.Text); } } } } catch (System.OperationCanceledException ex) { log.Info(ex.Message); } catch (System.IO.IOException ioEx) { log.Error(ioEx.Message); MessageBox.Show( Properties.Resources.ReadFileError, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } catch (Exception ex) { log.Error(ex.Message); MessageBox.Show(ex.Message, Properties.Resources.MessageBoxErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.btnCancelUplaod.Enabled = true; this.btnCancelUplaod.Text = Properties.Resources.UploadBtn; tokenSource.Dispose(); bWork = false; } } else { if (tokenSource != null) { tokenSource.Cancel(); log.Info("cancel upload."); bWork = false; this.pbUpload.Value = 0; } } }
/// <summary> /// Delete the given file /// </summary> /// <param name="dirEntry">Directory entry of the file to delete</param> /// <returns>A value which indicates if the deletion was successful</returns> public async Task <bool> DeleteFile(SeafDirEntry dirEntry) { dirEntry.ThrowOnNull(nameof(dirEntry)); return(await DeleteFile(dirEntry.LibraryId, dirEntry.Path)); }
/// <summary> /// Removes the file from the list of starred files /// </summary> /// <param name="dirEntry">The file to unstar</param> public async Task <bool> UnstarFile(SeafDirEntry dirEntry) { var request = new UnstarFileRequest(AuthToken, dirEntry.LibraryId, dirEntry.Path); return(await _webConnection.SendRequestAsync(ServerUri, request)); }
/// <summary> /// Removes the file from the list of starred files /// </summary> /// <param name="dirEntry">The file to unstar</param> public async Task <string> CreatShareLink(SeafDirEntry dirEntry, CancellationToken cancelToken, string password = null, string expire = null) { CreateShareLinkRequest req = new CreateShareLinkRequest(AuthToken, dirEntry.LibraryId, dirEntry.Path, password, expire); return(await webConnection.SendRequestAsync(ServerUri, req, cancelToken)); }