/// <summary> /// Creates the directory in the given library. The directory structure with all parents will be created as well. /// If the directory already exists, no error will be thrown. /// </summary> /// <param name="session">The seafile session.</param> /// <param name="library">The library, where the directory should be created in.</param> /// <param name="path">The directory to create.</param> public static async Task CreateDirectoryWithParents(this SeafSession session, SeafLibrary library, string path) { var currentPath = path; for (int i = 0; i <= path.Count(n => n == '/'); i++) { if (await ExistsDirectory(session, library, currentPath)) { break; } if (currentPath.Contains('/')) { currentPath = currentPath.Substring(0, currentPath.LastIndexOf('/')); } else { currentPath = string.Empty; } } var dirsToCreate = path.Substring(currentPath.Length) .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (dirsToCreate.Length > 0) { foreach (string dir in dirsToCreate) { currentPath = $"{currentPath}/{dir}"; await session.CreateDirectory(library, currentPath); } } }
public void Test_SessionFromToken() { var serverInfoMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ ""version"": ""4.0.6"", ""features"": [ ""seafile-basic"", ]}") }; // From Token automatically checks the token using the CheckAccountInfo() command var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"usage\": 26038531,\"total\": 104857600,\"email\": \"[email protected]\"}") }; var mockedConnection = new MockedSeafConnection(); mockedConnection.FakeResponseFor <GetServerInfoRequest>(serverInfoMessage); mockedConnection.FakeResponseFor <AccountInfoRequest>(message); var session = ExecuteSync(() => SeafSession.FromToken(mockedConnection, new Uri("http://www.example.com"), FakeToken)); Assert.IsNotNull(session); Assert.AreEqual(FakeToken, session.AuthToken); Assert.AreEqual("*****@*****.**", session.Username); }
public void Test_SessionFromUsernameAndToken() { var session = SeafSession.FromToken(new Uri("http://www.example.com"), "*****@*****.**", FakeToken); Assert.IsNotNull(session); Assert.AreEqual(FakeToken, session.AuthToken); Assert.AreEqual("*****@*****.**", session.Username); }
public async Task <SeafSession> GetSession() { if (!string.IsNullOrEmpty(Token)) { return(await SeafSession.FromToken(ServerUri, Token)); } return(await SeafSession.Establish(ServerUri, Username, Password.ToCharArray())); }
public void Run() { var serverInfo = SeafSession.GetServerInfo(_options.ServerUri).Result; Console.WriteLine($"Version: {serverInfo.Version}"); Console.WriteLine("Features:"); foreach (var feature in serverInfo.Features) { Console.WriteLine($"- {feature}"); } }
/// <summary> /// Checks if the given directory exists. /// </summary> /// <param name="session">The seafile session.</param> /// <param name="library">The library, which will searched for the directory.</param> /// <param name="path">The directory to check for existence.</param> public static async Task <bool> ExistsDirectory(this SeafSession session, SeafLibrary library, string path) { try { var dirs = await session.ListDirectory(library, path); return(dirs != null); } catch (Exception) { return(false); } }
public void Test_Sessionless_Ping() { var connection = new MockedSeafConnection(); var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("\"pong\"") }; connection.FakeResponseFor <PingRequest>(message); var result = ExecuteSync(() => SeafSession.Ping(connection, new Uri("http://www.example.com"))); Assert.IsTrue(result); }
public void Test_GetServerInfo() { var connection = new MockedSeafConnection(); var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ ""version"": ""4.0.6"", ""features"": [ ""seafile-basic"", ]}") }; connection.FakeResponseFor <GetServerInfoRequest>(message); var serverInfo = ExecuteSync(() => SeafSession.GetServerInfo(connection, new Uri("http://www.example.com"))); Assert.AreEqual("4.0.6", serverInfo.Version); Assert.IsTrue(serverInfo.Features.Contains("seafile-basic")); }
static async Task Test() { try { var serverUri = new Uri("", UriKind.Absolute); var username = ""; var password = "".ToCharArray(); // authenticate with the Seafile server and retrieve a Session var session = await SeafSession.Establish(serverUri, username, password); var ping = await session.Ping(); Console.WriteLine(session.ServerVersion); Console.WriteLine(ping); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Retrieves a library by name. /// </summary> /// <param name="session">The seafile session.</param> /// <param name="libraryName">The name of the library to get.</param> public static async Task <SeafLibrary> GetLibrary(this SeafSession session, string libraryName) { var libs = await session.ListLibraries(); return(libs.SingleOrDefault(n => n.Name == libraryName)); }
private static async Task SeafClientDemo(Uri host, string user, char[] pw) { try { // if the seafile server uses a self-signed certificate we accept it // for demonstration purposes ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, errors) => true; // try to connect to the seafile server using the given credentials Console.Write("Connecting..."); var session = await SeafSession.Establish(host, user, pw); Console.WriteLine("OK"); Console.WriteLine(); // ping the server Console.Write("Pinging the server..."); if (await session.Ping()) { Console.WriteLine("OK"); } else { Console.WriteLine("Failed"); } // retrieve user account info var info = await session.CheckAccountInfo(); Console.WriteLine("Account info: "); Console.WriteLine("User name: " + info.Name); Console.WriteLine("Space used: " + MiscUtils.FormatByteSize(info.Usage)); Console.WriteLine("Space total: " + (info.HasUnlimitedSpace ? "unlimited" : MiscUtils.FormatByteSize(info.Quota))); Console.WriteLine(""); // create a test library // Console.Write("Creating library..."); // var newLib = await session.CreateLibrary("TestLib", "123"); // Console.WriteLine("OK"); // groups Console.WriteLine("Groups:"); var groupList = await session.ListGroups(); foreach (var g in groupList) { Console.WriteLine(String.Format("{0:d} {1}", g.Id, g.Name)); } if (groupList.Count > 0) { var group = groupList.First(); //await session.AddGroupMember(group, "*****@*****.**"); //Console.WriteLine("added user to group"); var request = new ListGroupMembersRequest(session.AuthToken, group.Id, 120); var members = await session.SendRequest(request); Console.WriteLine("Group members:"); foreach (var m in members) { Console.WriteLine(" " + m.Name + " " + m.Email); } //await session.RemoveGroupMember(group, "*****@*****.**"); //Console.WriteLine("removed user from group"); //await session.DeleteGroup(group); //Console.WriteLine("deleted group: " + group.Name); //await session.RenameGroup(group, "changed name"); //Console.WriteLine("renamed group"); } else { var group = await session.AddGroup("new group"); Console.WriteLine("added group: " + group.Id + " - " + group.Name); } // default library var defLib = await session.GetDefaultLibrary(); Console.WriteLine("Default library: " + defLib.Name); // retrieve user's libraries & shared libraries var libs = await session.ListLibraries(); libs = libs.Union(await session.ListSharedLibraries()).ToList(); Console.WriteLine("Libraries:"); IList <string[]> lines = new List <string[]>(); lines.Add(new[] { "Modified", "Permission", "Name", "Owner" }); foreach (var lib in libs) { var permission = lib.Permission == SeafPermission.ReadOnly ? "r" : "rw"; lines.Add(new[] { lib.Timestamp.ToString(), permission, lib.Name, lib.Owner }); } Console.WriteLine(MiscUtils.PadElementsInLines(lines, 2)); // list directories recursively var listDirsRequest = new ListDirectoryEntriesRequest(session.AuthToken, defLib.Id, "/", true); var dirs = await session.SendRequest(listDirsRequest); foreach (var d in dirs) { Console.WriteLine(d.Path); } } catch (AggregateException ex) { if (ex.InnerException != null) { throw ex.InnerException; } } catch (SeafException ex) { Console.WriteLine("Failed"); Console.WriteLine("Request failed: " + ex.Message); } }