public async Task <HttpResponseMessage> Upload(string path, string filename, int length, string distinctName = null, int domainID = 0, bool debug = false) { if (debug) { Logger.Get().AppendFormat(" Upload distinctName:{0}", distinctName != null ? distinctName : "none"); Logger.Get().AppendFormat(" Upload domainID:{0}", domainID); } try { Logger.Get().AppendFormat(" Upload domainID:{0}", domainID); this.OnlyAllowInternalAccess(); byte[] buffer = await Request.Content.ReadAsByteArrayAsync(); if (buffer == null) { buffer = new byte[0]; } if (buffer.Length != length) { throw new ArgumentException(); } List <FileManagerBase> clients = FileManagerBase.GetAllManagers(distinctName, domainID); var action = new ActionBlock <FileManagerBase>(async client => { await client.Upload(path, filename, buffer); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = clients.Count, }); clients.ForEach((c) => action.Post(c)); action.Complete(); await action.Completion; return(new HttpResponseMessage() { Content = new StringContent("{ \"success\" : true }", Encoding.UTF8, "text/javascript") }); } catch (Exception ex) { Logger.Get().Append(ex); string json = string.Format("{{ \"success\" : false, \"error\" : \"{0}\" }}", HttpUtility.JavaScriptStringEncode(ex.Message)); return(new HttpResponseMessage() { Content = new StringContent(json, Encoding.UTF8, "text/javascript") }); } }
public async Task <HttpResponseMessage> List(string path, string distinctName = null, int domainID = 0, bool debug = false) { if (debug) { Logger.Get().AppendFormat("List distinctName:{0}", distinctName != null ? distinctName : "none"); } try { Logger.Get().AppendFormat(" List domainID:{0}", domainID); //this.OnlyAllowInternalAccess(); FileManagerBase client = FileManagerBase.GetPrimaryManager(distinctName, domainID); var list = await client.GetList(path); var array = list.Select(i => new { @isFolder = i.Item1, @filename = i.Item2 }) .OrderBy(i => i.isFolder) .OrderBy(i => i.filename) .ToArray(); JavaScriptSerializer jss = new JavaScriptSerializer(); string json = string.Format("{{ \"success\" : true, \"list\" : {0} }}" , jss.Serialize(array) ); return(new HttpResponseMessage() { Content = new StringContent(json, Encoding.UTF8, "text/javascript") }); } catch (Exception ex) { Logger.Get().Append(ex); string json = string.Format("{{ \"success\" : false, \"error\" : \"{0}\" }}", HttpUtility.JavaScriptStringEncode(ex.Message)); return(new HttpResponseMessage() { Content = new StringContent(json, Encoding.UTF8, "text/javascript") }); } }
public async Task <HttpResponseMessage> PrepareUpload(string path, string filename, int length, string distinctName = null, int domainID = 0) { try { Logger.Get().AppendFormat(" PrepareUpload domainID:{0}", domainID); Logger.Get().AppendFormat("PrepareUpload url:{0}", Request.RequestUri.ToString()); this.OnlyAllowInternalAccess(); List <FileManagerBase> clients = FileManagerBase.GetAllManagers(distinctName, domainID); var action = new ActionBlock <FileManagerBase>(async client => { await client.PrepareUpload(path, filename, length); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = clients.Count, }); clients.ForEach((c) => action.Post(c)); action.Complete(); await action.Completion; return(new HttpResponseMessage() { Content = new StringContent("{ \"success\" : true }", Encoding.UTF8, "text/javascript") }); } catch (Exception ex) { Logger.Get().Append(ex); string json = string.Format("{{ \"success\" : false, \"error\" : \"{0}\" }}", HttpUtility.JavaScriptStringEncode(ex.Message)); return(new HttpResponseMessage() { Content = new StringContent(json, Encoding.UTF8, "text/javascript") }); } }