protected GeneralTask(string type, bool create) : base(FileHelper.GetDataPath(DateTime.UtcNow.Shorten() + ".task"), type) { if (!create) { throw new NotSupportedException("You should call .ctor(id) if you are NOT going to create something!"); } ID = Path.GetFileNameWithoutExtension(FilePath); }
protected override void ExecuteCore() { SevenZipExtractor extractor = null; try { string directory = Target.Replace('/', '\\'), filePath = FileHelper.GetFilePath(directory), dataPath = FileHelper.GetDataPath(directory); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (!Directory.Exists(dataPath)) { Directory.CreateDirectory(dataPath); } extractor = new SevenZipExtractor(FileHelper.GetFilePath(Source)); var singleFileName = extractor.ArchiveFileNames.Count == 1 && extractor.ArchiveFileNames[0] == "[no name]" ? Path.GetFileNameWithoutExtension(Source) : null; FileCount = extractor.FilesCount; FileLength = extractor.ArchiveFileData.Sum(data => (long)data.Size); long nextLength = 0, nextFile = 0; extractor.FileExtractionStarted += (sender, e) => { ProcessedFileCount += nextFile; ProcessedFileLength += nextLength; nextLength = (long)e.FileInfo.Size; nextFile = 1; StartFile(FileHelper.Combine(directory, singleFileName ?? e.FileInfo.FileName)); }; extractor.FileExtractionFinished += (sender, e) => FinishFile(FileHelper.Combine(directory, singleFileName ?? e.FileInfo.FileName)); extractor.ExtractArchive(filePath); Finish(); } catch (SevenZipException) { if (extractor == null) { throw; } throw new AggregateException(extractor.Exceptions); } }
private void CopyDirectory(string domain, string source, string target) { CurrentFile = FileHelper.Combine(target, Path.GetFileName(source)); Save(); try { var root = XDocument.Parse(client.DownloadString( string.Format("http://{0}/Api/List/{1}", domain, source))).Root; if (root.GetAttributeValue("status") != "ok") { throw new ExternalException(root.GetAttributeValue("message")); } foreach (var element in root.Elements()) { var name = element.GetAttributeValue("name"); switch (element.Name.LocalName) { case "directory": var dir = FileHelper.Combine(target, name); Directory.CreateDirectory(FileHelper.GetFilePath(dir)); Directory.CreateDirectory(FileHelper.GetDataPath(dir)); CopyDirectory(domain, FileHelper.Combine(source, name), dir); break; case "file": CopyFile(domain, FileHelper.Combine(source, name), target); break; } } } catch (Exception exc) { ErrorMessage += string.Format("复制 /{0} 时发生了错误:{2}{1}{2}", target, exc.GetMessage(), Environment.NewLine); Save(); } }
protected GeneralTask(string id) : base(FileHelper.GetDataPath(id + ".task")) { ID = id; }
protected void Page_Load(object sender, EventArgs e) { var upload = Request.HttpMethod == "POST"; if (upload && !Request.GetUser().OperateFiles) { Response.StatusCode = 401; return; } var data = upload ? Request.Form : Request.QueryString; string id = data["resumableIdentifier"], path = FileHelper.Combine (RouteData.GetRelativePath(), data["resumableRelativePath"].ToValidPath(false)), filePath = FileHelper.GetFilePath(path), dataPath = FileHelper.GetDataFilePath(path), state = FileHelper.GetState(dataPath); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); Directory.CreateDirectory(Path.GetDirectoryName(dataPath)); if (state != TaskType.UploadTask || state == TaskType.UploadTask && new UploadTask(path).Identifier != id) { FileHelper.Delete(path); File.WriteAllBytes(filePath, new byte[0]); try { new UploadTask(path, id, int.Parse(data["resumableTotalChunks"]), long.Parse(data["resumableTotalSize"])).Save(); } catch (IOException) { } // another save in progress } var index = int.Parse(data["resumableChunkNumber"]) - 1; if (upload) { string basePath = FileHelper.GetDataPath(path), partSuffix = ".part" + index, partPath = basePath + ".incomplete" + partSuffix; try { using (var output = new FileStream(partPath, FileMode.Create, FileAccess.Write, FileShare.Read)) Request.Files[Request.Files.AllKeys.Single()].InputStream.CopyTo(output); File.Move(partPath, basePath + ".complete" + partSuffix); } catch { FileHelper.DeleteWithRetries(partPath); // delete imcomplete file } var task = new UploadTask(path); if (task.FinishedParts != task.TotalParts) { return; } using (var output = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) for (var i = 0; i < task.TotalParts; ++i) { using (var input = new FileStream( partPath = FileHelper.GetDataPath(path + ".complete.part" + i), FileMode.Open, FileAccess.Read, FileShare.Read)) input.CopyTo(output); FileHelper.DeleteWithRetries(partPath); } task.Finish(); } else { Response.StatusCode = File.Exists(FileHelper.GetDataPath(path + ".complete.part" + index)) ? 200 : 204; // test } }