public async Task <IActionResult> KirjaAttachment(string pageId, string attachmentFile)
        {
            // Check Data
            if (string.IsNullOrEmpty(pageId) || string.IsNullOrEmpty(attachmentFile))
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            KirjaPage page = await _pageDbAccess.GetPageById(pageId);

            if (page == null || page.Attachments == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound));
            }

            // Get Attachment
            KirjaPageAttachment attachment = FindAttachment(page, attachmentFile);

            if (attachment == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound));
            }

            // Return File
            Stream imageStream = _fileAccess.OpenFile(attachment.Filename);

            return(File(imageStream, attachment.MimeType, attachment.OriginalFilename));
        }
        public async Task <IActionResult> DeleteAttachment(string pageId, string attachmentFile)
        {
            // Check Data
            if (string.IsNullOrEmpty(pageId) || string.IsNullOrEmpty(attachmentFile))
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            KirjaPage page = await _pageDbAccess.GetPageById(pageId);

            if (page == null || page.Attachments == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound));
            }

            // Get Attachment
            KirjaPageAttachment attachment = FindAttachment(page, attachmentFile);

            if (attachment == null)
            {
                return(Ok(pageId));
            }

            // Delete Attachment
            try
            {
                await this.SetModifiedData(_userManager, page);

                page.Attachments.Remove(attachment);
                await _pageDbAccess.UpdatePage(page);

                _logger.LogInformation("Attachment deleted from page");

                _fileAccess.DeleteFile(attachment.Filename);
                _logger.LogInformation("Attachment file deleted");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Could not delete attachment");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }

            await _timelineService.AddTimelineEntry(TimelineEvent.KirjaAttachmentDeleted, page.Name, page.Id, attachment.OriginalFilename);

            return(Ok(pageId));
        }
        public async Task <IActionResult> UploadPageAttachment(string id)
        {
            // Validate Date
            if (Request.Form.Files.Count != 1)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, _localizer["OnlyOneFileAllowed"]));
            }

            IFormFile uploadFile      = Request.Form.Files[0];
            string    fileContentType = uploadFile.ContentType;
            bool      mimeTypeAllowed = false;

            foreach (string curAllowedMimeType in _allowedAttachmentMimeTypes)
            {
                if (Regex.IsMatch(fileContentType, curAllowedMimeType))
                {
                    mimeTypeAllowed = true;
                    break;
                }
            }

            if (!mimeTypeAllowed)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, _localizer["FileTypeNotAllowed"]));
            }

            // Get Page
            KirjaPage page = await _pageDbAccess.GetPageById(id);

            if (page == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound));
            }

            // Save File
            string fileName = string.Empty;

            try
            {
                using (Stream fileStream = _fileAccess.CreateFile(uploadFile.FileName, out fileName))
                {
                    uploadFile.CopyTo(fileStream);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Could not upload file");
                return(StatusCode((int)HttpStatusCode.InternalServerError, _localizer["CouldNotUploadFile"]));
            }

            // Save File to page
            if (page.Attachments == null)
            {
                page.Attachments = new List <KirjaPageAttachment>();
            }

            KirjaPageAttachment pageAttachment = new KirjaPageAttachment();

            pageAttachment.OriginalFilename = uploadFile.FileName;
            pageAttachment.Filename         = fileName;
            pageAttachment.MimeType         = fileContentType;

            page.Attachments.Add(pageAttachment);
            page.Attachments = page.Attachments.OrderBy(pa => pa.OriginalFilename).ToList();

            try
            {
                await this.SetModifiedData(_userManager, page);

                await _pageDbAccess.UpdatePage(page);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Could not save page after file upload");
                _fileAccess.DeleteFile(fileName);
                return(StatusCode((int)HttpStatusCode.InternalServerError, _localizer["CouldNotUploadFile"]));
            }

            await _timelineService.AddTimelineEntry(TimelineEvent.KirjaAttachmentAdded, page.Name, page.Id, pageAttachment.OriginalFilename);

            return(Ok(pageAttachment));
        }