public async Task <FileSystemResult <IDirectory> > CreateDirectoryAsync(string name, Dictionary <string, object> properties) { if (properties == null) { properties = new Dictionary <string, object>(); } Json.Metadata j = new Json.Metadata(); if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase))) { j.modifiedDate = (DateTime)properties.First(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase))) { j.createdDate = (DateTime)properties.First(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase))) { j.createdBy = (string)properties.First(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)).Value; } else { j.createdBy = "CloudFileSystem"; } j.description = j.name = name; j.isShared = false; j.kind = "FOLDER"; j.parents = new List <string> { Id }; string url = AmazonCreateDirectory.FormatRest(FS.OAuth.EndPoint.MetadataUrl, Guid.NewGuid().ToString().Replace("-", string.Empty)); FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(j, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })), "application/json"); if (ex.IsOk) { AmazonDirectory dir = new AmazonDirectory(this.FullName, FS) { Parent = this }; dir.SetData(ex.Result); FS.Refs[dir.FullName] = dir; IntDirectories.Add(dir); return(new FileSystemResult <IDirectory>(dir)); } return(new FileSystemResult <IDirectory>(ex.Error)); }
// ReSharper disable once CyclomaticComplexity internal async Task <FileSystemResult <IFile> > InternalCreateFile(string name, string type, bool overwrite, AmazonObject parent, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties) { if (readstream.Length == 0) { throw new ArgumentException("input stream must have length"); } string url = AmazonUpload.FormatRest(FS.OAuth.EndPoint.ContentUrl, "?suppress=deduplication"); Json.Metadata j = null; if (!overwrite) { if (properties == null) { properties = new Dictionary <string, object>(); } j = new Json.Metadata(); j.version = 0; j.contentProperties = new Json.ContentProperties(); j.contentProperties.size = readstream.Length; j.contentProperties.version = 0; j.contentProperties.extension = Path.GetExtension(name); string n = Extensions.ContentFromExtension(j.contentProperties.extension); j.contentProperties.contentType = !string.IsNullOrEmpty(n) ? n : "application/octet-stream"; if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase))) { j.modifiedDate = (DateTime) properties.First( a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase))) { j.createdDate = (DateTime) properties.First( a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value; } if (properties.Any(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase))) { j.createdBy = (string) properties.First( a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)).Value; } else { j.createdBy = "CloudFileSystem"; } if (properties.Any(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase))) { j.contentProperties.md5 = (string) properties.First(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase)) .Value; } j.description = j.name = name; j.isShared = false; j.kind = type; j.parents = new List <string>(); j.parents.Add(parent.Id); } HttpRequestMessage msg = new HttpRequestMessage(overwrite ? HttpMethod.Put : HttpMethod.Post, url); string boundary = "--" + Guid.NewGuid().ToString(); msg.Headers.UserAgent.ParseAdd(FS.OAuth.UserAgent); MultipartFormDataContent ct = new MultipartFormDataContent(boundary); if (!overwrite) { string meta = JsonConvert.SerializeObject(j); StringContent sc = new StringContent(meta, Encoding.UTF8); ct.Add(sc, "metadata"); } StreamContent ssc = new StreamContent(readstream); ct.Add(ssc, "file", name); msg.Content = ct; HttpClientHandler handler = new HttpClientHandler(); ProgressMessageHandler progresshandler = new ProgressMessageHandler(handler); progresshandler.HttpSendProgress += (a, b) => { FileProgress u = new FileProgress(); u.Percentage = b.ProgressPercentage; u.TotalSize = b.TotalBytes ?? 0; u.TransferSize = b.BytesTransferred; progress.Report(u); }; handler.AllowAutoRedirect = true; handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; await FS.CheckExpirations(); HttpClient cl = new HttpClient(progresshandler); HttpResponseMessage response = null; try { response = await cl.SendAsync(msg, token); if (response.StatusCode == HttpStatusCode.Created) { string dd = await response.Content.ReadAsStringAsync(); string parentpath = string.Empty; AmazonDirectory dir = parent as AmazonDirectory; if (dir == null || !dir.IsRoot) { parentpath = FullName; } AmazonFile file = new AmazonFile(parentpath, FS) { Parent = dir }; file.SetData(dd); progress.Report(new FileProgress { Percentage = 100, TotalSize = file.Size, TransferSize = file.Size }); return(new FileSystemResult <IFile>(file)); } return(new FileSystemResult <IFile>("Http Error : " + response.StatusCode)); } catch (Exception e) { return(new FileSystemResult <IFile>("Exception Error : " + e.Message)); } finally { response?.Dispose(); cl?.Dispose(); handler?.Dispose(); progresshandler?.Dispose(); msg?.Dispose(); } }