Example #1
0
 public async Task <ReturnResult <Attachment> > AddOrUpdateAttachment(
     IFormFile file,
     AttachmentTypesEnm attType,
     Guid?attachmentId = null,
     string title      = null)
 {
     return(await _attachmentRepository.AddOrUpdateAttachmentAsync(_appSettingsService.AttachmentsPath, file,
                                                                   attType));
 }
        public async Task <ReturnResult <Guid> > AddAttachment(AttachmentTypesEnm attachmentTypes, string attachmentsPath, IFormFile file, string title = null, bool saveToDB = true)
        {
            var result    = new ReturnResult <Guid>();
            var attResult = await this.AddOrUpdateAttachmentAsync(attachmentsPath, file, attachmentTypes, null, title, saveToDB);

            if (!attResult.IsValid)
            {
                result.Merge(attResult);
                return(result);
            }

            result.Value = attResult.Value.Id;
            return(result);
        }
Example #3
0
 public async Task <Attachment> AddOrUpdateAttachment(
     string fileName,
     string contentType,
     byte[] fileBytes,
     AttachmentTypesEnm attType,
     Guid?attachmentId    = null,
     string titleAr       = null,
     string titleEn       = null,
     string descriptionAr = null,
     string descriptionEn = null,
     int?itemOrder        = null)
 {
     return(await _attachmentRepository.AddOrUpdateAttachmentAsync(_appSettingsService.AttachmentsPath, fileName,
                                                                   contentType,
                                                                   fileBytes,
                                                                   attType));
 }
        public async Task <ReturnResult <Attachment> > AddOrUpdateAttachmentAsync(
            string attachmentsPath,
            IFormFile file,
            AttachmentTypesEnm attType,
            Guid?attachmentId = null,
            string title      = null, bool saveToDB = true)
        {
            var result = new ReturnResult <Attachment>();

            if (file == null)
            {
                result.AddErrorItem(string.Empty, CommonResources.FileZeroLengthErrorMessage);
                return(result);
            }

            if (file.Length <= 0)
            {
                result.AddErrorItem(string.Empty, CommonResources.FileZeroLengthErrorMessage);
                return(result);
            }

            if (saveToDB &&
                string.IsNullOrEmpty(attachmentsPath))
            {
                throw new Exception(
                          "File can not be saved. Current Settings is. SaveFileToDatabase=true and Attachment Path is Missing");
            }

            var fileBytes = new byte[file.Length];

            ////file.InputStream.Read(fileBytes, 0, file.Length);
            var ms = new MemoryStream();

            file.OpenReadStream().CopyTo(ms);

            result.Value = await this.AddOrUpdateAttachmentAsync(attachmentsPath,
                                                                 file.FileName,
                                                                 file.ContentType,
                                                                 ms.ToArray(),
                                                                 attType,
                                                                 attachmentId,
                                                                 null,
                                                                 title);

            return(result);
        }
Example #5
0
 public async Task <ReturnResult <Guid> > AddAttachment(AttachmentTypesEnm attachmentType, IFormFile file, string title = null)
 {
     return(await _attachmentRepository.AddAttachment(attachmentType, _appSettingsService.AttachmentsPath, file, title));
 }
        public async Task <Attachment> AddOrUpdateAttachmentAsync(string attachmentsPath,
                                                                  string fileName,
                                                                  string contentType,
                                                                  byte[] fileBytes,
                                                                  AttachmentTypesEnm attType,
                                                                  Guid?attachmentId    = null,
                                                                  string titleAr       = null,
                                                                  string titleEn       = null,
                                                                  string descriptionAr = null,
                                                                  string descriptionEn = null,
                                                                  int?itemOrder        = null, bool saveToDB = true)
        {
            var isUpdateFile = attachmentId.HasValue && attachmentId.Value != Guid.Empty;

            var attachment = isUpdateFile
                                 ? await this.GetByIdAsync(attachmentId.Value)
                                 : new Attachment
            {
                Id = Guid.NewGuid().AsSequentialGuid()
            };

            if (attachment == null)
            {
                throw new Exception("The Attachment File You are trying to update Does Not Exist in the database");
            }

            if (attachment.AttachmentContent == null)
            {
                attachment.AttachmentContent = new AttachmentContent();
            }

            attachment.AttachmentContent.FileContent = fileBytes;

            attachment.TitleAr          = titleAr;
            attachment.TitleEn          = titleEn;
            attachment.DescriptionAr    = descriptionAr ?? attachment.DescriptionAr;
            attachment.DescriptionEn    = descriptionEn ?? attachment.DescriptionEn;
            attachment.ContentType      = contentType;
            attachment.Extension        = new FileInfo(fileName).Extension;
            attachment.FileName         = fileName;
            attachment.AttachmentTypeId = (int)attType;
            if (contentType.StartsWith("image/"))
            {
                attachment.Thumbnail = this.GenerateThumbnail(fileBytes);
            }

            // in updating delete old file
            if (isUpdateFile)
            {
                this.DeleteAttachmentFromFileSystem(attachment.FilePath, attachmentsPath);
            }
            attachment.FilePath = saveToDB
                                      ? null
                                      : this.SaveAttachmentToFileSystem(attachment, attachmentsPath);
            attachment.AttachmentContent.Id          = attachment.Id;
            attachment.AttachmentContent.FileContent =
                saveToDB ? fileBytes : null;

            if (!isUpdateFile)
            {
                _dbContext.Attachments.Add(attachment);
            }

            return(attachment);
        }