コード例 #1
0
ファイル: v1.cs プロジェクト: Agash/sdsetup-backend
 public ActionResult FetchGeneratedZip(string uuid)
 {
     try
     {
         if (Program.generatedZips.ContainsKey(uuid))
         {
             Program.generatedZips[uuid].StopTimeout();
             DeletingFileStream stream = Program.generatedZips[uuid];
             Program.generatedZips[uuid] = null;
             Program.generatedZips.Remove(uuid);
             if (stream == null)
             {
                 //StatusCode 410 Gone: The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent (since UUID..).
                 return(StatusCode(410, "Expired"));
             }
             string zipname = ("SDSetup(" + DateTime.Now.ToShortDateString() + ").zip").Replace("-", ".").Replace("_", ".");
             Response.Headers["Content-Disposition"] = "filename=" + zipname;
             return(new FileStreamResult(stream, "application/zip"));
         }
         else
         {
             return(StatusCode(410, "Expired"));
         }
     }
     catch (Exception)
     {
         Program.generatedZips[uuid] = null;
         return(StatusCode(410, "Expired"));
     }
 }
コード例 #2
0
ファイル: v1.cs プロジェクト: Agash/sdsetup-backend
        public static Stream ZipFromFilestreams(KeyValuePair <string, string>[] files, string uuid)
        {
            DeletingFileStream outputMemStream = new DeletingFileStream(Program.Temp + "/" + Guid.NewGuid().ToString().Replace("-", "").ToLower(), FileMode.Create, uuid);
            ZipOutputStream    zipStream       = new ZipOutputStream(outputMemStream);

            zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

            foreach (KeyValuePair <string, string> f in files)
            {
                ZipEntry newEntry = new ZipEntry(f.Key);
                newEntry.DateTime = DateTime.Now;
                zipStream.PutNextEntry(newEntry);
                FileStream fs = new FileStream(f.Value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                fs.CopyTo(zipStream, 4096);
                //StreamUtils.Copy(fs, zipStream, new byte[4096]);
                fs.Close();
                zipStream.CloseEntry();
            }


            zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
            zipStream.Close();               // Must finish the ZipOutputStream before using outputMemStream.

            outputMemStream.Position = 0;

            return(outputMemStream);
        }
コード例 #3
0
 public ActionResult FetchGeneratedZip(string uuid)
 {
     try {
         if (Program.generatedZips.ContainsKey(uuid))
         {
             Program.generatedZips[uuid].StopTimeout();
             DeletingFileStream stream = Program.generatedZips[uuid];
             Program.generatedZips[uuid] = null;
             Program.generatedZips.Remove(uuid);
             if (stream == null)
             {
                 return(new ObjectResult("Expired"));
             }
             string zipname = ("SDSetup(" + DateTime.Now.ToShortDateString() + ").zip").Replace("-", ".").Replace("_", ".");
             Response.Headers["Content-Disposition"] = "filename=" + zipname;
             return(new FileStreamResult(stream, "application/zip"));
         }
         else
         {
             return(new ObjectResult("Expired"));
         }
     } catch (Exception e) {
         Program.generatedZips[uuid] = null;
         return(new ObjectResult("Something went wrong (the zip may have expired): \n\n" + e.Message));
     }
 }
コード例 #4
0
ファイル: v1.cs プロジェクト: Agash/sdsetup-backend
        public ActionResult FetchZip([FromHeader] FetchZipInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(400, "Invalid input"));
            }

            if (Program.uuidLocks.Contains(model.UUID))
            {
                return(StatusCode(400, "UUID " + model.UUID + " locked"));
            }

            if (!Program.validChannels.Contains(model.Channel))
            {
                return(StatusCode(400, "Invalid channel"));
            }

            if (!Directory.Exists(Program.Files + "/" + model.PackageSet))
            {
                return(StatusCode(400, "Invalid packageset"));
            }

            if (System.IO.File.Exists(Program.Files + "/" + model.PackageSet + "/.PRIVILEGED.FLAG") && !Program.IsUuidPriveleged(model.UUID))
            {
                return(StatusCode(401, "You do not have access to that packageset"));
            }

            string tempdir = Program.Temp + "/" + model.UUID;

            try
            {
                Program.uuidLocks.Add(model.UUID);

                string[] requestedPackages = model.Packages.Split(';');
                List <KeyValuePair <string, string> > files = new List <KeyValuePair <string, string> >();
                foreach (string k in requestedPackages)
                {
                    //sanitize input
                    if (k.Contains("/") || k.Contains("/") || k.Contains("..") || k.Contains("~") || k.Contains("%"))
                    {
                        Program.uuidLocks.Remove(model.UUID);
                        return(StatusCode(400, "hackerman"));
                    }

                    if (Directory.Exists(Program.Files + "/" + model.PackageSet + "/" + k + "/" + model.Channel))
                    {
                        foreach (string f in EnumerateAllFiles(Program.Files + "/" + model.PackageSet + "/" + k + "/" + model.Channel))
                        {
                            if (model.Client == "hbswitch")
                            {
                                if (f.StartsWith(Program.Files + "/" + model.PackageSet + "/" + k + "/" + model.Channel + "/sd"))
                                {
                                    files.Add(new KeyValuePair <string, string>(f.Replace(Program.Files + "/" + model.PackageSet + "/" + k + "/" + model.Channel + "/sd", ""), f));
                                }
                            }
                            else
                            {
                                files.Add(new KeyValuePair <string, string>(f.Replace(Program.Files + "/" + model.PackageSet + "/" + k + "/" + model.Channel, ""), f));
                            }
                        }
                    }
                }

                DeletingFileStream stream = (DeletingFileStream)ZipFromFilestreams(files.ToArray(), model.UUID);

                Program.generatedZips[model.UUID] = stream;
                stream.Timeout(30000);

                Program.uuidLocks.Remove(model.UUID);
                return(Ok("READY"));
            }
            catch (Exception)
            {
                Program.uuidLocks.Remove(model.UUID);
                return(StatusCode(500, "Internal server error occurred"));
            }
        }
コード例 #5
0
        public ActionResult FetchZip()
        {
            //wtf microsoft use a string array
            Microsoft.Extensions.Primitives.StringValues _uuid;
            Microsoft.Extensions.Primitives.StringValues _packageset;
            Microsoft.Extensions.Primitives.StringValues _channel;
            Microsoft.Extensions.Primitives.StringValues _packages;
            Microsoft.Extensions.Primitives.StringValues _client;

            if (!Request.Headers.TryGetValue("SDSETUP-UUID", out _uuid))
            {
                return(new StatusCodeResult(400));
            }
            if (!Request.Headers.TryGetValue("SDSETUP-PACKAGESET", out _packageset))
            {
                return(new StatusCodeResult(400));
            }
            if (!Request.Headers.TryGetValue("SDSETUP-CHANNEL", out _channel))
            {
                return(new StatusCodeResult(400));
            }
            if (!Request.Headers.TryGetValue("SDSETUP-PACKAGES", out _packages))
            {
                return(new StatusCodeResult(400));
            }
            Request.Headers.TryGetValue("SDSETUP-CLIENT", out _client); // optional, currently only used by homebrew app to specify it only wants SD folder


            string uuid       = _uuid[0];
            string packageset = _packageset[0];
            string channel    = _channel[0];
            string packages   = _packages[0];
            string client     = null;

            if (_client.Count > 0 && !String.IsNullOrWhiteSpace(_client[0]))
            {
                client = _client[0];
            }


            if (Program.uuidLocks.Contains(uuid))
            {
                return(new ObjectResult("UUID " + uuid + " locked"));
            }
            else if (!Program.validChannels.Contains(channel))
            {
                return(new ObjectResult("Invalid channel"));
            }
            else if (!Directory.Exists((Program.Files + "/" + packageset).AsPath()))
            {
                return(new ObjectResult("Invalid packageset"));
            }
            else if (System.IO.File.Exists((Program.Files + "/" + packageset + "/.PRIVILEGED.FLAG").AsPath()) && !Program.IsUuidPriveleged(uuid))
            {
                return(new ObjectResult("You do not have access to that packageset"));
            }
            else
            {
                string tempdir = (Program.Temp + "/" + uuid).AsPath();
                try {
                    Program.uuidLocks.Add(uuid);

                    string[] requestedPackages = packages.Split(';');
                    List <KeyValuePair <string, string> > files = new List <KeyValuePair <string, string> >();
                    foreach (string k in requestedPackages)
                    {
                        //sanitize input
                        if (k.Contains("/") || k.Contains("\\") || k.Contains("..") || k.Contains("~") || k.Contains("%"))
                        {
                            Program.uuidLocks.Remove(uuid);
                            return(new ObjectResult("hackerman"));
                        }

                        if (Directory.Exists((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath()))
                        {
                            foreach (string f in EnumerateAllFiles((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath()))
                            {
                                if (client == "hbswitch")
                                {
                                    if (f.StartsWith((Program.Files + "/" + packageset + "/" + k + "/" + channel + "/sd").AsPath()))
                                    {
                                        files.Add(new KeyValuePair <string, string>(f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel + "/sd").AsPath(), ""), f));
                                    }
                                }
                                else
                                {
                                    files.Add(new KeyValuePair <string, string>(f.Replace((Program.Files + "/" + packageset + "/" + k + "/" + channel).AsPath(), ""), f));
                                }
                            }
                        }

                        Program.dlStats.IncrementPackageDownloadCount(k);
                    }

                    Program.dlStats.AllTimeBundles++;
                    DeletingFileStream stream = (DeletingFileStream)ZipFromFilestreams(files.ToArray(), uuid);

                    Program.generatedZips[uuid] = stream;
                    stream.Timeout(30000);

                    Program.uuidLocks.Remove(uuid);
                    return(new ObjectResult("READY"));
                } catch (Exception) {
                    Program.uuidLocks.Remove(uuid);
                    return(new ObjectResult("Internal server error occurred"));
                }
            }
        }