コード例 #1
0
        private bool CopyFile(string domain, string source, string target, bool logging = true)
        {
            var targetFile = FileHelper.Combine(target, Path.GetFileName(source));

            CurrentFile = targetFile;
            try
            {
                var root = XDocument.Parse(client.DownloadString(
                                               string.Format("http://{0}/Api/Details/{1}", domain, source))).Root;
                if (root.GetAttributeValue("status") != "ok")
                {
                    throw new ExternalException(root.GetAttributeValue("message"));
                }
                Save();
                Program.OfflineDownload(string.Format("http://{0}/Download/{1}", domain, source), target, client);
                var file = root.Element("file");
                FileHelper.SetDefaultMime(FileHelper.GetDataFilePath(targetFile), file.GetAttributeValue("mime"));
                ProcessedFileCount++;
                ProcessedFileLength += file.GetAttributeValue <long>("size");
                Save();
                return(true);
            }
            catch (Exception exc)
            {
                if (logging)
                {
                    ErrorMessage += string.Format("复制 /{0} 时发生了错误:{2}{1}{2}", target, exc.GetMessage(),
                                                  Environment.NewLine);
                    Save();
                }
                return(false);
            }
        }
コード例 #2
0
 protected void NewFolder(object sender, EventArgs e)
 {
     if (!CurrentUser.OperateFiles)
     {
         Response.StatusCode = 401;
         return;
     }
     FileHelper.CreateDirectory(FileHelper.Combine(RelativePath, Hidden.Value.UrlDecode()));
     Response.Redirect(Request.RawUrl);
 }
コード例 #3
0
 protected void Copy(object sender, EventArgs e)
 {
     if (!CurrentUser.OperateFiles)
     {
         Response.StatusCode = 401;
         return;
     }
     foreach (var source in SelectedPaths)
     {
         FileHelper.Copy(source,
                         FileHelper.Combine(Hidden.Value.UrlDecode(), Path.GetFileName(source)), false);
     }
     Response.Redirect(Request.RawUrl);
 }
コード例 #4
0
 protected void FileCommand(object source, RepeaterCommandEventArgs e)
 {
     if (!CurrentUser.OperateFiles)
     {
         Response.StatusCode = 401;
         return;
     }
     switch (e.CommandName)
     {
     case "Rename":
         FileHelper.Move(FileHelper.Combine(RelativePath,
                                            ((HtmlInputHidden)e.Item.FindControl("Hidden")).Value),
                         FileHelper.Combine(RelativePath, Hidden.Value.UrlDecode()));
         Response.Redirect(Request.RawUrl);
         break;
     }
 }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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();
            }
        }
コード例 #7
0
        private void NiGuan(string path, XContainer result)
        {
            Response.Write("<!-- Processing");
            Response.Flush();
            var url = Rbase64.Decode(Request.QueryString["Url"].UrlDecode());

            if (string.IsNullOrWhiteSpace(url))
            {
                return;
            }
            foreach (var video in Net.YouTube.Video.GetVideoFromLink(url))
            {
                var element = new XElement("video", new XAttribute("title", video.Title),
                                           new XAttribute("url", video.Url), new XAttribute("author", video.Author),
                                           new XAttribute("keywords", string.Join(", ", video.Keywords)),
                                           new XAttribute("rating", video.AverageRating), new XAttribute("viewCount", video.ViewCount),
                                           new XAttribute("uploadTime", video.UploadTime.Ticks), new XAttribute("length", video.Length));
                foreach (var link in video.Downloads)
                {
                    var e = new XElement("download", new XAttribute("type", link.ToString()),
                                         new XAttribute("information", link.Properties));
                    if (link.UrlUnavailableException == null)
                    {
                        e.SetAttributeValue("link",
                                            string.Format("{0}://{1}/Task/Create/Offline/{2}?Url={3}", Request.Url.Scheme,
                                                          Request.Url.Host, FileHelper.Combine(path, (link.Parent.Title + link.Extension)
                                                                                               .ToValidPath()), Rbase64.Encode(link.GetUrl(link.Parent.Title))));
                    }
                    element.Add(e);
                }
                result.Add(element);
                Response.Write('.');    // prevent the thread from getting killed, how evil I am MUAHAHA
                Response.Flush();
            }
            Response.Write(" -->" + Environment.NewLine);
        }
コード例 #8
0
        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
            }
        }