public bool IsExits(File file) { lock (_sych) { return(_files.ContainsKey(file.Id)); } }
public File Select(int id) { File xxx = null; _files.TryGetValue(id, out xxx); return(xxx); }
void SaveToDisk(File file) { new ThreadSafe(() => { lock (_lockDisk) { var saveTo = _indexedDir + file.Id.ToString() + ".dfil"; using (var sw = new StreamWriter(saveTo, false)) { var serialize = new JavaScriptSerializer().Serialize(file); sw.Write(serialize); sw.Flush(); sw.Close(); } } }).Start(); }
public void Add(File file) { var id = file.PathOnWeb.UrlToHashCode(); file.Id = id; if (!_files.ContainsKey(id)) { lock (_sych) { if (!_files.ContainsKey(id)) { _data.Add(file); _files.Add(id, file); } } SaveToDisk(file); } }
public void Update(File file) { lock (_sych) { var f = _data.FirstOrDefault(i => i.Id == file.Id); if (f != null) { f.DownloadCompleted = file.DownloadCompleted; f.DownloadInprogress = file.DownloadInprogress; f.MergerCompleted = file.MergerCompleted; f.Owner = file.Owner; f.PathOnDisk = file.PathOnDisk; f.PathOnWeb = file.PathOnWeb; f.Href = file.Href; _files[file.Id] = f; } } SaveToDisk(file); }
public void DownloadFile(File file) { file.DownloadCompleted = false; file.DownloadInprogress = true; DownloadInprogress(this, file); try { var req = new HttpRequest(file.PathOnWeb); var pathFile = ""; HttpWebResponse response; Stream res = req.DownloadData(out response); if (response != null) { var ext = Files.GetFileExtension(file.PathOnWeb, response.ContentType); TryCreateDirPath(file.PathOnWeb, ext, out pathFile); using (var f = System.IO.File.OpenWrite(pathFile)) { CopyStream(res, f); f.Flush(); f.Close(); } file.PathOnDisk = pathFile; file.DownloadCompleted = true; file.DownloadInprogress = false; req.TryAbort(); DownloadComplete(this, file); } else { file.DownloadError = true; DownloadComplete(this, file); } } catch { file.DownloadError = true; DownloadComplete(this, file); } }
static FileRepository() { _indexedDir = _root.Trim('/') + "/" + _indexedDir.Trim('/') + "/"; if (!Directory.Exists(_indexedDir)) { Directory.CreateDirectory(_indexedDir); } new ThreadSafe(() => { var files = Directory.GetFiles(_indexedDir); foreach (var f in files) { var item = new File(); lock (_sych) { item.Id = Files.GetIdFromFileIndexed(f); item.LazyLoad(); _data.Add(item); _files.Add(item.Id, item); } } }).Start(); }