Example #1
0
    /*
     * Protect JS Files.
     * 
     * Example: protect first.js and second.js. Use domain=localhost, trial expires during 1 day. Use default template (see defaultTemplate in code).
     * 
     * JSCrambler.lib.Protect(
     *  new JSCrambler.FileItemLow[] { 
     *    new JSCrambler.FileItem(@"d:\temp\first.js", @"d:\temp\first.charMin.js"),
     *    new JSCrambler.FileItem(@"d:\temp\second.js", @"d:\temp\second.charMin.js"),
     *  }, 
     *  "localhost", DateTime.UtcNow.AddDays(1));

    */
    public static void Protect(FileItemLow[] _files, string _domain_lock = null, DateTime? _expiration_date = null, Template templ = null) {

      //upload files for protection to new project and get project compId
      var upload = createProject(_files, templ, _domain_lock, _expiration_date);

      //extract data from JSON
      var projId = upload["id"].ToString();
      var parts = (IEnumerable<dynamic>)upload["sources"];
      projectFile[] fileIds = parts.Select(p => new projectFile() {
        guid = p["id"],
        extension = p["extension"],
        name = p["filename"]
      }).OfType<projectFile>().ToArray();

      //wait for project finishing
      while (true) {
        Thread.Sleep(1000);
        //get project info
        var proj = getProjectInfo(projId);
        //Error => see details in JSCrambler Web site
        string error = proj["error_id"] ?? 0.ToString();
        if (error != null && error != "0") throw new Exception("Error: " + error + ", Message: " + proj["error_message"]);
        //Not yet finished => try again after 1000 msec
        string finished_at = proj["finished_at"] ?? "".ToString();
        if (string.IsNullOrEmpty(finished_at)) continue;
        break;
      }

      // A. pres project files
      //Finished => get protected data
      addFileData(projId, fileIds);
      //save protected data
      foreach (var jsId in fileIds)
        _files[jsId.idx].writeResult(jsId.data);

      // B. pres project ZIP
      //Unzip protected files
      //Stream stream = getProjectZip(email);
      //using (var archive = new ZipArchive(stream)) {
      //  foreach (var file in archive.Entries) {
      //    var idx = jsIds.First(j => j.name == file.Name).idx; 
      //    using (var zip = file.Open())
      //    using (var unzip = _files[idx].gerDestinationStream())
      //      zip.CopyTo(unzip);
      //  }
      //}

      //Delete project
      deleteProject(projId);
    }
Example #2
0
 //upload files for protection to new project
 public static dynamic createProject(FileItemLow[] _files, Template templ, string _domain_lock, DateTime? _expiration_date) {
   return Utf8BytesToJson(new Par(_files, templ, _domain_lock, _expiration_date).runHttpRequest());
 }
Example #3
0
 //POST constructor
 internal Par(FileItemLow[] _files, Template templ, string _domain_lock, DateTime? _expiration_date) {
   if (templ == null) templ = Lib.defaultTemplate;
   foreach (var fld in Template.Fields()) fld.SetValue(this, fld.GetValue(templ)); //copy params from template
   domain_lock = _domain_lock;
   expiration_date = _expiration_date == null ? null : ((DateTime)_expiration_date).ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
   files = _files;
   finishPar(methods.post, "/code.json");
 }