Example #1
0
        /// <summary>
        /// Uploads file to Cloudinary
        /// </summary>
        /// <param name="fileToUpload">File to be uploaded</param>
        /// <param name="userId">Identifier of sumbitter</param>
        /// <returns>Absolute uri to uploaded resource</returns>
        public string UploadFile(FileToUploadDto fileToUpload, int userId)
        {
            Stream stream = new MemoryStream(fileToUpload.FileInBytes);

            var uploadParams = new ImageUploadParams()
            {
                File      = new FileDescription(fileToUpload.Filename, stream),
                PublicId  = Guid.NewGuid().ToString(),
                Folder    = $"reports/attachments/{userId}/",
                Overwrite = false
            };

            var uploadResult = _cloudinary.Upload(uploadParams);

            return(uploadResult.SecureUrl.AbsoluteUri);
        }
        /// <summary>
        /// Creates attachment if file has been received. Returns null otherwise.
        /// </summary>
        /// <param name="file">Attachment data</param>
        /// <param name="userId">Identifier of submitter</param>
        /// <returns>Generated attachment entity or null</returns>
        private AttachmentEntity CreateAttachment(FileToUploadDto file, int userId)
        {
            if (file != null && file.FileInBytes != null)
            {
                string attachmentUrl = _cloudinaryManager.UploadFile(file, userId);

                AttachmentEntity attachment = new AttachmentEntity
                {
                    Name = file.Filename,
                    Url  = attachmentUrl
                };

                return(attachment);
            }

            return(null);
        }