public List<IFileEntry> List() { LitJson.JsonData data; try { System.Net.HttpWebRequest req = CreateRequest("", "t=json"); req.Method = System.Net.WebRequestMethods.Http.Get; using (System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse()) { int code = (int)resp.StatusCode; if (code < 200 || code >= 300) //For some reason Mono does not throw this automatically throw new System.Net.WebException(resp.StatusDescription, null, System.Net.WebExceptionStatus.ProtocolError, resp); //HACK: We need the LitJSON to use Invariant culture, otherwise it cannot parse doubles System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; try { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream())) data = LitJson.JsonMapper.ToObject(sr); } finally { try { System.Threading.Thread.CurrentThread.CurrentCulture = ci; } catch { } } } } catch (System.Net.WebException wex) { //Convert to better exception if (wex.Response as System.Net.HttpWebResponse != null) if ((wex.Response as System.Net.HttpWebResponse).StatusCode == System.Net.HttpStatusCode.Conflict || (wex.Response as System.Net.HttpWebResponse).StatusCode == System.Net.HttpStatusCode.NotFound) throw new Interface.FolderMissingException(string.Format(Strings.TahoeBackend.MissingFolderError, m_url, wex.Message), wex); throw; } if (data.Count < 2 || !data[0].IsString || (string)data[0] != "dirnode") throw new Exception(string.Format(Strings.TahoeBackend.UnexpectedJsonFragmentType, data.Count < 1 ? "<null>" : data[0], "dirnode")); if (!data[1].IsObject) throw new Exception(string.Format(Strings.TahoeBackend.UnexpectedJsonFragmentType, data[1], "Json object")); if (!(data[1] as System.Collections.IDictionary).Contains("children") || !data[1]["children"].IsObject || !(data[1]["children"] is System.Collections.IDictionary)) throw new Exception(string.Format(Strings.TahoeBackend.UnexpectedJsonFragmentType, data[1], "children")); List<IFileEntry> files = new List<IFileEntry>(); foreach (string key in ((System.Collections.IDictionary)data[1]["children"]).Keys) { LitJson.JsonData entry = data[1]["children"][key]; if (!entry.IsArray || entry.Count < 2 || !entry[0].IsString || !entry[1].IsObject) continue; bool isDir = ((string)entry[0]) == "dirnode"; bool isFile = ((string)entry[0]) == "filenode"; if (!isDir && !isFile) continue; FileEntry fe = new FileEntry(key); fe.IsFolder = isDir; if (((System.Collections.IDictionary)entry[1]).Contains("metadata")) { LitJson.JsonData fentry = entry[1]["metadata"]; if (fentry.IsObject && ((System.Collections.IDictionary)fentry).Contains("tahoe")) { fentry = fentry["tahoe"]; if (fentry.IsObject && ((System.Collections.IDictionary)fentry).Contains("linkmotime")) { try { fe.LastModification = ((DateTime)(Library.Utility.Utility.EPOCH + TimeSpan.FromSeconds((double)fentry["linkmotime"])).ToLocalTime()); } catch { } } } } if (((System.Collections.IDictionary)entry[1]).Contains("size")) { try { if (entry[1]["size"].IsInt) fe.Size = (int)entry[1]["size"]; else if (entry[1]["size"].IsLong) fe.Size = (long)entry[1]["size"]; } catch {} } files.Add(fe); } return files; }
public static FileEntry ParseLine(string line) { Match m = MatchLine(line); if (m == null) return null; FileEntry f = new FileEntry(m.Groups["name"].Value); string time = m.Groups["timestamp"].Value; string dir = m.Groups["dir"].Value; //Unused //string permission = m.Groups["permission"].Value; if (dir != "" && dir != "-") f.IsFolder = true; else f.Size = long.Parse(m.Groups["size"].Value); DateTime t; if (DateTime.TryParse(time, out t)) f.LastAccess = f.LastModification = t; return f; }
public List<IFileEntry> List() { List<IFileEntry> ls = new List<IFileEntry>(); PreAuthenticate(); if (!System.IO.Directory.Exists(m_path)) throw new FolderMissingException(string.Format(Strings.FileBackend.FolderMissingError, m_path)); foreach (string s in System.IO.Directory.GetFiles(m_path)) { System.IO.FileInfo fi = new System.IO.FileInfo(s); ls.Add(new FileEntry(fi.Name, fi.Length, fi.LastAccessTime, fi.LastWriteTime)); } foreach (string s in System.IO.Directory.GetDirectories(m_path)) { System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(s); FileEntry fe = new FileEntry(di.Name, 0, di.LastAccessTime, di.LastWriteTime); fe.IsFolder = true; ls.Add(fe); } return ls; }
public List<IFileEntry> List() { System.Net.HttpWebRequest req = CreateRequest(""); req.Method = "PROPFIND"; req.Headers.Add("Depth", "1"); req.ContentType = "text/xml"; req.ContentLength = PROPFIND_BODY.Length; using (System.IO.Stream s = req.GetRequestStream()) s.Write(PROPFIND_BODY, 0, PROPFIND_BODY.Length); try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); using (System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse()) { int code = (int)resp.StatusCode; if (code < 200 || code >= 300) //For some reason Mono does not throw this automatically throw new System.Net.WebException(resp.StatusDescription, null, System.Net.WebExceptionStatus.ProtocolError, resp); if (!string.IsNullOrEmpty(m_debugPropfindFile)) { using (System.IO.FileStream fs = new System.IO.FileStream(m_debugPropfindFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)) Utility.Utility.CopyStream(resp.GetResponseStream(), fs, false); doc.Load(m_debugPropfindFile); } else doc.Load(resp.GetResponseStream()); } System.Xml.XmlNamespaceManager nm = new System.Xml.XmlNamespaceManager(doc.NameTable); nm.AddNamespace("D", "DAV:"); List<IFileEntry> files = new List<IFileEntry>(); m_filenamelist = new List<string>(); foreach (System.Xml.XmlNode n in doc.SelectNodes("D:multistatus/D:response/D:href", nm)) { //IIS uses %20 for spaces and %2B for + //Apache uses %20 for spaces and + for + string name = System.Web.HttpUtility.UrlDecode(n.InnerText.Replace("+", "%2B")); string cmp_path; //TODO: This list is getting ridiculous, should change to regexps if (name.StartsWith(m_url)) cmp_path = m_url; else if (name.StartsWith(m_rawurl)) cmp_path = m_rawurl; else if (name.StartsWith(m_rawurlPort)) cmp_path = m_rawurlPort; else if (name.StartsWith(m_path)) cmp_path = m_path; else if (name.StartsWith(m_sanitizedUrl)) cmp_path = m_sanitizedUrl; else if (name.StartsWith(m_reverseProtocolUrl)) cmp_path = m_reverseProtocolUrl; else continue; if (name.Length <= cmp_path.Length) continue; name = name.Substring(cmp_path.Length); long size = -1; DateTime lastAccess = new DateTime(); DateTime lastModified = new DateTime(); bool isCollection = false; System.Xml.XmlNode stat = n.ParentNode.SelectSingleNode("D:propstat/D:prop", nm); if (stat != null) { System.Xml.XmlNode s = stat.SelectSingleNode("D:getcontentlength", nm); if (s != null) size = long.Parse(s.InnerText); s = stat.SelectSingleNode("D:getlastmodified", nm); if (s != null) try { //Not important if this succeeds lastAccess = lastModified = DateTime.Parse(s.InnerText, System.Globalization.CultureInfo.InvariantCulture); } catch { } s = stat.SelectSingleNode("D:iscollection", nm); if (s != null) isCollection = s.InnerText.Trim() == "1"; else isCollection = (stat.SelectSingleNode("D:resourcetype/D:collection", nm) != null); } FileEntry fe = new FileEntry(name, size, lastAccess, lastModified); fe.IsFolder = isCollection; files.Add(fe); m_filenamelist.Add(name); } return files; } catch (System.Net.WebException wex) { if (wex.Response as System.Net.HttpWebResponse != null && ((wex.Response as System.Net.HttpWebResponse).StatusCode == System.Net.HttpStatusCode.NotFound || (wex.Response as System.Net.HttpWebResponse).StatusCode == System.Net.HttpStatusCode.Conflict)) throw new Interface.FolderMissingException(string.Format(Strings.WEBDAV.MissingFolderError, m_path, wex.Message), wex); throw; } }