/// <summary> /// Deletes the file binary from the file system. /// </summary> /// <param name="attachmentId">Attachment ID</param> /// <param name="name">Returning the attachment name</param> protected bool DeleteFromFileSystem(int attachmentId, ref string name) { // Delete the file in file system var ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, false); if (ai != null) { name = ai.AttachmentName; // Ensure the binary column first (check if exists) DataSet ds = AttachmentInfoProvider.GetAttachments() .WhereEquals("AttachmentID", attachmentId) .BinaryData(true) .Columns("CASE WHEN AttachmentBinary IS NULL THEN 0 ELSE 1 END AS HasBinary"); if (!DataHelper.DataSourceIsEmpty(ds)) { bool hasBinary = ValidationHelper.GetBoolean(ds.Tables[0].Rows[0][0], false); if (!hasBinary) { // Copy the binary data to database ai.AttachmentBinary = AttachmentBinaryHelper.GetAttachmentBinary((DocumentAttachment)ai); ai.Generalized.UpdateData(); } // Delete the file from the disk AttachmentBinaryHelper.DeleteFile(ai.AttachmentGUID, SiteInfoProvider.GetSiteName(ai.AttachmentSiteID), true); return(true); } } return(false); }
#pragma warning restore 1998 private void DownloadAttachment(DocumentAttachment attachment, IExecutionContext context) { var fileName = $"{StatiqHelper.AttachmentPath}/{attachment.AttachmentName}"; var destination = context.FileSystem.GetOutputFile(fileName); if (!destination.Exists) { var thread = new CMSThread(() => { try { var binary = AttachmentBinaryHelper.GetAttachmentBinary(attachment); using (Stream fileStream = destination.OpenWrite(true)) { long initialPosition = fileStream.Position; fileStream.Write(binary, 0, binary.Length); long length = fileStream.Position - initialPosition; fileStream.SetLength(length); } } catch (Exception e) { Console.WriteLine(e.Message); } }); thread.Start(); } }
public async Task SyncAttachment(AttachmentInfo attachment) { if (attachment == null) { throw new ArgumentNullException(nameof(attachment)); } // Do not synchronize variants if (attachment.IsVariant()) { return; } try { var fullFileName = attachment.AttachmentName; SyncLog.LogEvent(EventType.INFORMATION, "KenticoKontentPublishing", "SYNCATTACHMENT", fullFileName); var externalId = GetAttachmentExternalId(attachment.AttachmentGUID); var fileName = GetShortenedFileName(fullFileName); var title = string.IsNullOrEmpty(attachment.AttachmentTitle) ? fileName : attachment.AttachmentTitle; var existing = await GetAsset(externalId); // TODO - Consider detection by something more sophisticated than file size + name, but be careful, last modified may be off due to metadata changes if ((existing == null) || (attachment.AttachmentSize != existing.Size) || (fileName != existing.FileName)) { // Upload new var data = AttachmentBinaryHelper.GetAttachmentBinary((DocumentAttachment)attachment); var fileReference = await UploadBinaryFile(data, attachment.AttachmentMimeType, fileName); await UpsertAsset(externalId, title, attachment.AttachmentDescription, fileReference.Id); } else { // Update metadata of existing await UpsertAsset(externalId, title, attachment.AttachmentDescription, existing.FileReference.Id); } } catch (Exception ex) { SyncLog.LogException("KenticoKontentPublishing", "SYNCATTACHMENT", ex); throw; } }
/// <summary> /// Copies the file binary to the file system. /// </summary> /// <param name="attachmentId">Attachment ID</param> /// <param name="name">Returning the attachment name</param> protected bool CopyToFileSystem(int attachmentId, ref string name) { // Copy the file from database to the file system var ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, true); if (ai != null) { name = ai.AttachmentName; // Ensure the physical file AttachmentBinaryHelper.EnsurePhysicalFile((DocumentAttachment)ai); return(true); } return(false); }
/// <summary> /// Deletes the file binary from the database. /// </summary> /// <param name="attachmentId">Attachment ID</param> /// <param name="name">Returning the attachment name</param> protected bool DeleteFromDatabase(int attachmentId, ref string name) { // Delete the file in database and ensure it in the file system var ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, false); if (ai != null) { name = ai.AttachmentName; AttachmentBinaryHelper.EnsurePhysicalFile((DocumentAttachment)ai); // Clear the binary data ai.AttachmentBinary = null; ai.Generalized.UpdateData(); return(true); } return(false); }
/// <summary> /// Copies the file binary to the database. /// </summary> /// <param name="attachmentId">Attachment ID</param> /// <param name="name">Returning the attachment name</param> protected bool CopyToDatabase(int attachmentId, ref string name) { // Copy the file from file system to the database var ai = AttachmentInfoProvider.GetAttachmentInfo(attachmentId, true); if (ai != null) { name = ai.AttachmentName; if (ai.AttachmentBinary == null) { // Ensure the binary data ai.AttachmentBinary = AttachmentBinaryHelper.GetAttachmentBinary((DocumentAttachment)ai); ai.Generalized.UpdateData(); return(true); } } return(false); }
/// <summary> /// Grid external data bound handler. /// </summary> protected object gridFiles_OnExternalDataBound(object sender, string sourceName, object parameter) { // Get the data row view from parameter DataRowView drv = null; if (parameter is DataRowView) { drv = (DataRowView)parameter; } else if (parameter is GridViewRow) { // Get data from the grid view row drv = (DataRowView)((GridViewRow)parameter).DataItem; } // Get the action button var btn = sender as CMSGridActionButton; switch (sourceName) { case "delete": { // Delete action int siteId = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0); string siteName = SiteInfoProvider.GetSiteName(siteId); Guid guid = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty); string extension = ValidationHelper.GetString(drv["AttachmentExtension"], ""); // Check if the file is in DB bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false); // Check if the file is in the file system bool fs = false; string path = AttachmentBinaryHelper.GetFilePhysicalPath(siteName, guid.ToString(), extension); if (File.Exists(path)) { fs = true; } // If the file is present in both file system and database, delete is allowed if (fs && db) { var filesLocationType = GetFilesLocationType(siteId); // If the files are stored in file system or use both locations, delete is allowed in database if (filesLocationType != FilesLocationTypeEnum.Database) { btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteindatabase'"); btn.ToolTip = "Delete from database"; return(parameter); } // Else the files are stored in database, delete is allowed in file system btn.OnClientClick = btn.OnClientClick.Replace("'delete'", "'deleteinfilesystem'"); btn.ToolTip = "Delete from file system"; return(parameter); } btn.Visible = false; } break; case "copy": { // Delete action int siteId = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0); string siteName = SiteInfoProvider.GetSiteName(siteId); Guid guid = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty); string extension = ValidationHelper.GetString(drv["AttachmentExtension"], ""); // Check if the file is in DB bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false); // Check if the file is in the file system bool fs = false; string path = AttachmentBinaryHelper.GetFilePhysicalPath(siteName, guid.ToString(), extension); if (File.Exists(path)) { fs = true; } var filesLocationType = GetFilesLocationType(siteId); // If the file is stored in file system and the file is not present in database, copy to database is allowed if (fs && !db && (filesLocationType == FilesLocationTypeEnum.Both)) { btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytodatabase'"); btn.ToolTip = "Copy to database"; //btn.ImageUrl = return(parameter); } // If the file is stored in database and the file is not present in file system, copy to file system is allowed if (db && !fs && filesLocationType != FilesLocationTypeEnum.Database) { btn.OnClientClick = btn.OnClientClick.Replace("'copy'", "'copytofilesystem'"); btn.ToolTip = "Copy to file system"; //btn.ImageUrl = return(parameter); } btn.Visible = false; } break; case "name": return(GetAttachmentHtml(new DataRowContainer(drv))); case "size": // File size return(DataHelper.GetSizeString(ValidationHelper.GetInteger(parameter, 0))); case "yesno": // Yes / No return(UniGridFunctions.ColoredSpanYesNo(parameter)); case "site": { int siteId = ValidationHelper.GetInteger(parameter, 0); if (siteId > 0) { SiteInfo si = SiteInfoProvider.GetSiteInfo(siteId); if (si != null) { return(si.DisplayName); } } return(null); } case "storedinfilesystem": { // Delete action int siteId = ValidationHelper.GetInteger(drv["AttachmentSiteID"], 0); string siteName = SiteInfoProvider.GetSiteName(siteId); Guid guid = ValidationHelper.GetGuid(drv["AttachmentGUID"], Guid.Empty); string extension = ValidationHelper.GetString(drv["AttachmentExtension"], ""); // Check if the file is in DB bool db = ValidationHelper.GetBoolean(drv["HasBinary"], false); // Check if the file is in the file system bool fs = false; string path = AttachmentBinaryHelper.GetFilePhysicalPath(siteName, guid.ToString(), extension); if (File.Exists(path)) { fs = true; } return(UniGridFunctions.ColoredSpanYesNo(fs)); } } return(parameter); }