Example #1
0
        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;
            }
        }