public static async Task <BaseModel> CreateAsync(IDriver driver, IDirectory directory, RootVolume volume) { if (directory == null) { throw new ArgumentNullException("directory"); } if (volume == null) { throw new ArgumentNullException("volume"); } if (volume.RootDirectory == directory.FullName) { bool hasSubdirs = false; var subdirs = await directory.GetDirectoriesAsync(); foreach (var item in subdirs) { if (!item.Attributes.HasFlag(FileAttributes.Hidden)) { hasSubdirs = true; break; } } var response = new RootModel { Mime = "directory", Dirs = hasSubdirs ? (byte)1 : (byte)0, Hash = volume.VolumeId + HttpEncoder.EncodePath(directory.Name), Read = 1, Write = volume.IsReadOnly ? (byte)0 : (byte)1, Locked = volume.IsLocked ? (byte)1 : (byte)0, Name = volume.Alias, Size = 0, UnixTimeStamp = (long)(DateTime.UtcNow - _unixOrigin).TotalSeconds, VolumeId = volume.VolumeId }; return(response); } else { string parentPath = directory.Parent.FullName.Substring(volume.RootDirectory.Length); string relativePath = directory.FullName.Substring(volume.RootDirectory.Length).TrimEnd(Path.DirectorySeparatorChar); var response = new DirectoryModel { Mime = "directory", ContainsChildDirs = (await directory.GetDirectoriesAsync()).Count() > 0 ? (byte)1 : (byte)0, Hash = volume.VolumeId + HttpEncoder.EncodePath(relativePath), Read = 1, Write = volume.IsReadOnly ? (byte)0 : (byte)1, Locked = ((volume.LockedFolders != null && volume.LockedFolders.Any(f => f == directory.Name)) || volume.IsLocked) ? (byte)1 : (byte)0, Size = 0, Name = directory.Name, UnixTimeStamp = (long)(await directory.LastWriteTimeUtcAsync - _unixOrigin).TotalSeconds, ParentHash = volume.VolumeId + HttpEncoder.EncodePath(parentPath.Length > 0 ? parentPath : directory.Parent.Name) }; return(response); } }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs) { var response = new AddResponseModel(); if (!string.IsNullOrEmpty(name)) { var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(this, newDir, path.RootVolume)); } if (dirs.Any()) { foreach (var dir in dirs) { var dirName = dir.StartsWith("/") ? dir.Substring(1) : dir; var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, dirName)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(this, newDir, path.RootVolume)); var relativePath = newDir.FullName.Substring(path.RootVolume.RootDirectory.Length); response.Hashes.Add(new KeyValuePair <string, string>($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath))); } } return(await Json(response)); }
public static string UrlEncode(string s, Encoding Enc) { if (s == null) { return(null); } if (s == string.Empty) { return(string.Empty); } bool flag = false; int length = s.Length; for (int i = 0; i < length; i++) { char c = s[i]; if (((((c < '0') || ((c < 'A') && (c > '9'))) || ((c > 'Z') && (c < 'a'))) || (c > 'z')) && !HttpEncoder.NotEncoded(c)) { flag = true; break; } } if (!flag) { return(s); } byte[] bytes = new byte[Enc.GetMaxByteCount(s.Length)]; int count = Enc.GetBytes(s, 0, s.Length, bytes, 0); return(Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, count))); }
public static string Create(OAuthRequest request, IDictionary <string, string> requestParams) { string signature; var ts = string.Empty; var cs = string.IsNullOrEmpty(request.Consumer.ConsumerSecret) ? string.Empty : HttpEncoder.UrlEncode(request.Consumer.ConsumerSecret); if (request.Token != null) { ts = string.IsNullOrEmpty(request.Token.Secret) ? string.Empty : HttpEncoder.UrlEncode(request.Token.Secret); } var key = string.Format("{0}&{1}", cs, ts); switch (request.SignatureMethod) { case OAuthSignatureMethods.PlainText: signature = key; break; case OAuthSignatureMethods.HmacSha1: var url = NormalizeUrl(request.RequestEndpoint); var baseString = CreateBaseString(url, requestParams, request.Parameters); var hmacSha1 = new HMACSHA1 { Key = Encoding.ASCII.GetBytes(key) }; var hashedBytes = hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(baseString)); signature = Convert.ToBase64String(hashedBytes); break; default: const string message = "OAuth signature method is not yet supported."; throw new NotSupportedException(message); } return(HttpEncoder.UrlEncode(signature)); }