private static void HandleTouchRequest(string path, IHttpResponseDelegate response) { var comp = path.Split('/').Last(); var uid = uint.Parse(comp); var npid = 0x110000100000000 | uid; var fileID = npid.ToString("x16") + "_mpdata"; var document = new NPFile(); document.Id = fileID; document.SetData(new byte[12288]); CDatabase.Database.CreateDocument(document, new MindTouch.Tasking.Result <NPFile>()).Wait(); var responseText = "Touched."; var responseBytes = Encoding.ASCII.GetBytes(responseText); response.OnResponse(new HttpResponseHead() { Status = "200 OK", Headers = new Dictionary <string, string>() { { "Content-Length", responseBytes.Length.ToString() }, { "Content-Type", "text/plain" } } }, new BufferedProducer(responseBytes)); }
private static void MigrateFiles() { if (File.Exists("data/migration_files_002_done")) { return; } //var files = Directory.GetFiles("data/priv2", "*.*", SearchOption.AllDirectories); var directories = Directory.GetDirectories("data/priv2/00"); foreach (var dir in directories) { var subdirectories = Directory.GetDirectories(dir); foreach (var subdir in subdirectories) { var files = Directory.GetFiles(subdir); foreach (var file in files) { var match = Regex.Match(file.Replace("\\", "/"), "00/([0-9a-f]{3})/([0-9a-f]{16})/(.*)$"); if (!match.Success) { continue; } if (file.Contains("iw4_")) { continue; } var npid = long.Parse(match.Groups[2].Value, System.Globalization.NumberStyles.AllowHexSpecifier); var filename = match.Groups[3].Value; var id = npid.ToString("x16") + "_" + filename; var isNew = true; var npfile = new NPFile(); if (Database.DocumentExists(id)) { isNew = false; npfile = Database.GetDocument <NPFile>(id); } npfile.Id = id; npfile.SetData(File.ReadAllBytes(file)); if (isNew) { Database.CreateDocument <NPFile>(npfile); } else { Database.UpdateDocument <NPFile>(npfile); } Console.WriteLine(id); } } } File.WriteAllText("data/migration_files_002_done", DateTime.Now.ToString()); }
public override void Process(NPHandler client) { var fileName = Message.fileName; var fileData = Message.fileData; var npid = (long)Message.npid; var fileID = npid.ToString("x16") + "_" + fileName; //var fsFile = StorageUtils.GetFilename(fileName, npid); _client = client; _fileName = fileName; _npid = npid; if (!client.Authenticated) { ReplyWithError(1); return; } if (client.NPID != (long)npid) { ReplyWithError(1); return; } // are we allowed to write this type of file? if (!_fileHooks.ContainsKey(fileName)) { ReplyWithError(1); return; } Log.Info(string.Format("Got a request for writing {0} bytes to file {1} for user {2}.", fileData.Length, fileName, npid.ToString("X16"))); CDatabase.Database.GetDocument <NPFile>(fileID, new MindTouch.Tasking.Result <NPFile>()).WhenDone(res => { var document = new NPFile(); document.Id = fileID; var isNew = true; if (res.Value != null) { string backupFile; try { int result = _fileHooks[fileName](fileData, Convert.FromBase64String(res.Value.FileData), out backupFile); if (result > 0) { ReplyWithError(result); return; } } catch (Exception ex) { Log.Error(ex.ToString()); ReplyWithError(2); return; } isNew = false; document = res.Value; } document.SetData(fileData); MindTouch.Tasking.Result <NPFile> res2; if (isNew) { res2 = CDatabase.Database.CreateDocument(document, new MindTouch.Tasking.Result <NPFile>()); } else { res2 = CDatabase.Database.UpdateDocument(document, new MindTouch.Tasking.Result <NPFile>()); } res2.WhenDone(success => { ReplyWithError(0); }, exception => { Log.Error(exception.ToString()); ReplyWithError(2); }); }); }