private void SaveUploadedFileMetadata(IFormCollection form) { //Read data string name = form["file_name"]; string type = form["file_type"]; string description = form["description"]; string[] tags = form["tags"].ToString().Split(','); DateTime time = new DateTime(int.Parse(form["dt_year"]), int.Parse(form["dt_month"]), int.Parse(form["dt_day"]), 12, 0, 0); //Get the pathname string pathname = info.FullName + "/" + name; //Create metadata var metadata = new ArchivedFileMetadata { template_type = type, tags = tags, description = description, custom_data = new Dictionary <string, string>(), time = time, time_approx = true }; File.WriteAllText(pathname + ".meta", JsonConvert.SerializeObject(metadata)); //Refresh site.RefreshObjects(); }
public ArchivedFile(ArchiveSite site, ArchivedDirectory dir, string metadataFilePath) : base(site) { //Set this.parent = dir; this.metadataFilePath = metadataFilePath; //Load metadata metadata = JsonConvert.DeserializeObject <ArchivedFileMetadata>(File.ReadAllText(metadataFilePath)); //Get uploaded date if (metadata.uploaded_date.HasValue) { uploadedDate = metadata.uploaded_date.Value; } else { uploadedDate = new FileInfo(metadataFilePath).LastWriteTimeUtc; metadata.uploaded_date = uploadedDate; } //Get file if it's not remote if (!metadata.is_remote) { file = new FileInfo(metadataFilePath.Substring(0, metadataFilePath.Length - 5)); //trim off .meta } //Create path path = dir.path + GetName(); //Check if we have rich metadata if (metadata.rich_metadata == null) { rich_metadata_status = MetadataStatus.NOT_GENERATED; } else { rich_metadata_status = MetadataStatus.OK; } //Add this.site.AddStoredObject(this); }
public async Task OnAdminUploadRequest(HttpContext e) { //Check if we need to show the form if (e.Request.Method.ToUpper() != "POST") { //Set headers e.Response.ContentType = "text/html"; //Make default tags string tags = ""; if (subFiles.Count > 0) { for (int i = 0; i < subFiles[0].metadata.tags.Length; i += 1) { tags += subFiles[0].metadata.tags[i]; if (i != subFiles[0].metadata.tags.Length - 1) { tags += ","; } } } //Show form await TemplateManager.WriteTemplate(e.Response.Body, "ADMIN.UPLOAD_FILE", new Dictionary <string, string> { { "PATH", path }, { "TAGS", tags } }); } else { //Run var form = await e.Request.ReadFormAsync(); //Read data string name = form["file_name"]; string type = form["file_type"]; string description = form["description"]; string[] tags = form["tags"].ToString().Split(','); DateTime time = new DateTime(int.Parse(form["dt_year"]), int.Parse(form["dt_month"]), int.Parse(form["dt_day"]), 12, 0, 0); //Get the pathname string pathname = info.FullName + "/" + name; //Check if (File.Exists(pathname)) { e.Response.ContentType = "text/html"; await e.WriteString("<span style=\"color: red;\">The file you're attempting to create already exists. Aborting!</span>"); return; } //Save file using (FileStream fs = new FileStream(pathname, FileMode.Create)) using (Stream f = form.Files["file_upload"].OpenReadStream()) await f.CopyToAsync(fs); //Create metadata var metadata = new ArchivedFileMetadata { template_type = type, tags = tags, description = description, custom_data = new Dictionary <string, string>(), time = time, time_approx = true }; File.WriteAllText(pathname + ".meta", JsonConvert.SerializeObject(metadata)); //Refresh site.RefreshObjects(); //Redirect to this e.Response.Redirect(site.config.client_pathname_prefix + path_urlsafe, false); } }