public void DeleteUnusedAttachments() { bool verbose = true; var files = Directory.GetFiles(Web.MapPath(Web.Attachments), "*.*", SearchOption.AllDirectories); Web.Flush("Indexing files...<br>"); var attachments = new List <string>(); foreach (var file in files) { var attachmentName = file.RightFrom(Web.MapPath(Web.Attachments)).ToLower(); if (!attachmentName.Contains("todelete\\")) { attachments.Add(attachmentName.Replace("\\", "/")); } } Web.Flush("Total " + attachments.Count + " files...<br>"); foreach (var tableName in BewebData.GetTableNames()) { Web.Flush("Table: " + tableName + "<br>"); var sql = new Sql("select top 1 * from ", tableName.SqlizeName()); var fields = new DelimitedString(","); using (var reader = sql.GetReader()) { if (verbose) { Web.Flush("Checking structure...<br>"); } int rec = 0; if (reader.HasRows) { int visibleFieldCount = reader.VisibleFieldCount; for (int i = 0; i < visibleFieldCount; i++) // for each column { string dataType = reader.GetDataTypeName(i).ToLower(); string fieldName = reader.GetName(i).ToLower(); bool isAttach = (dataType.Contains("varchar") && (fieldName.Contains("attachment") || fieldName.Contains("picture"))); bool isRich = ((dataType.Contains("varchar") || dataType.Contains("text")) && (fieldName.Contains("html") || fieldName.Contains("body") || fieldName.Contains("text"))); if (isAttach || isRich) { fields += fieldName; } } } } if (fields.IsBlank) { Web.Flush("Skipping table as no relevant field names<br>"); } else { sql = new Sql("select " + fields.ToString() + " from ", tableName.SqlizeName()); Web.Flush("Searching table... " + sql.Value + "<br>"); using (var reader = sql.GetReader()) { Web.Flush("Scanning records...<br>"); int rec = 0; foreach (DbDataRecord record in reader) { rec++; var foundAttachments = new List <string>(); int visibleFieldCount = reader.VisibleFieldCount; for (int i = 0; i < visibleFieldCount; i++) // for each column { string fieldName = record.GetName(i).ToLower(); bool isAttach = ((fieldName.Contains("attachment") || fieldName.Contains("picture"))); if (!record.IsDBNull(i)) { string fieldValue = record.GetString(i); if (fieldValue.IsNotBlank()) { foreach (var attachmentName in attachments) { if (fieldValue == attachmentName || (!isAttach && fieldValue.ToLower().Contains(attachmentName))) { if (verbose) { Web.WriteLine(" Found: " + attachmentName + " in " + tableName); } foundAttachments.Add(attachmentName); } } } attachments.RemoveAll(a => foundAttachments.Contains(a)); } } if (rec % 100 == 0) { Web.Flush("Scanned: " + rec + " records<br>"); } } } } } Web.Flush("Finished checking. Located " + attachments.Count + " unused attachments<br>"); int totalSize = 0; int cnt = 0; foreach (var attachmentName in attachments) { var size = FileSystem.GetFileSizeBytes(Web.Attachments + attachmentName); totalSize += size; Web.WriteLine("Not found: " + attachmentName + " " + Fmt.FileSize(size, 2)); if (Request["doit"] == "1") { FileSystem.Move(Web.Attachments + attachmentName, Web.Attachments + "todelete", false); } cnt++; if (cnt % 100 == 0) { Web.Flush("Archived: " + cnt + " files<br>"); } } Web.WriteLine("Total size: " + Fmt.FileSize(totalSize, 2)); //DirectoryInfo di = new DirectoryInfo(Server.MapPath(Web.Attachments+"trademe/")); ////read all files into a list //var files = new List<string>(); //foreach (var file in di.EnumerateFiles()) { // Web.Write(file.Name); // files.Add(file.Name); //} ////read db records, remove from files list if they exist in the database //var sql = new Sql("select * from trademelisting"); //foreach (DbDataRecord row in sql.GetReader()) { // //int pageid = (int)row["pageID"]; // //C:\data\dev\web\Honda\PublicServices\attachments\trademe\HAS1001_1709195_9_tn.jpg // //C:\data\dev\web\Honda\PublicServices\attachments\trademe\HAS1001_1709181_8.jpg // // // for(int scanIndex=0;scanIndex<20;scanIndex++){ // var name = row["DealerCode"]+"_"+row["ID"]+"_"+scanIndex; // var nameThumb = name+"_tn.jpg"; // if(files.Contains(nameThumb))files.Remove(nameThumb); // name = name+".jpg"; // if(files.Contains(name))files.Remove(name); // } //} ////delete all files remaining in the list //foreach(var file in files) { // string filename = Server.MapPath(Web.Attachments+"trademe/")+file; // FileSystem.Delete(filename); //} Web.InfoMessage = "Deleted Unused Images and Attachment Files"; }
private void CompareAttachments() { var diskAttachments = (List <string>)Session["diskAttachments"]; var dbAttachments = (HashSet <string>)Session["dbAttachments"]; diskAttachments.RemoveAll(dbAttachments.Contains); var count = diskAttachments.Count; Session["diskAttachments"] = null; Session["dbAttachments"] = null; Session["unusedAttachments"] = diskAttachments; var attachmentsFolder = Web.MapPath(Web.Attachments); int totalBytes = 0; var fileList = new StringBuilder("["); foreach (var attachment in diskAttachments) { var f = new FileInfo(Path.Combine(attachmentsFolder, attachment)); totalBytes += f.Length.ToInt(); if (fileList.Length > 1) { fileList.Append(","); } fileList.Append("\"" + attachment + "\""); } fileList.Append("]"); Web.Write("{\"success\": true, \"attachmentsUrl\": \"" + Web.Attachments + "\", \"files\": " + fileList + ", \"result\":\"" + count + " files were marked to be deleted" + (count > 0 ? " (" + Fmt.FileSize(totalBytes, 2) + "). <br/><a href='#' onclick='Vacuum.showFiles();return false;'>Show files</a> | <a href='#' onclick='Vacuum.moveFiles();return false;'>Move to attachments/unused_files</a>" : "") + "\"}"); }
private string ImageUploader(string type) { string fileName = null; int filesize = 0; try { var file = Request.Files[0]; if (file.ContentLength > 0) { const string subfolder = "paste/"; FileSystem.CreateFolder(Web.Attachments + subfolder); fileName = subfolder + FileSystem.GetUniqueFilename(Web.Attachments + subfolder, file.FileName.RightFrom("\\"), 50); var metaData = new DefaultPictureMetaData() { ThumbnailWidth = 260, ThumbnailHeight = 175, IsThumbnailExact = false, IsThumbnailCropped = false, PreviewWidth = 60, IsExact = false, IsCropped = false, Height = Util.GetSetting("MCEUploadedImageHeight", "960").ToInt(960), Width = Util.GetSetting("MCEUploadedImageWidth", "960").ToInt(960) }; fileName = ImageProcessing.ResizeImageUsingMetaData(fileName, metaData, file); filesize = file.ContentLength; } else { throw new Exception("No file selected"); } } catch (Exception ex) { return("{\"success\": false, \"error\": \"" + ex.Message.Replace("\\", "\\\\") + "\"}"); } if (type == "redactor") { return("{\"filelink\": \"" + Web.Attachments + "paste/" + Path.GetFileName(fileName) + "\"}"); } else { return("{\"success\": true, \"filename\": \"" + Path.GetFileName(fileName) + "\", \"filepath\": \"paste/" + Path.GetFileName(fileName) + "\", \"filesize\": \"" + Fmt.FileSize(filesize, 2) + "\"}"); } }