Esempio n. 1
0
        public async Task <AttachmentUploadDataResponseModel> PostAttachment(string id, [FromBody] AttachmentRequestModel request)
        {
            var idGuid = new Guid(id);
            var userId = _userService.GetProperUserId(User).Value;
            var cipher = request.AdminRequest ?
                         await _cipherRepository.GetOrganizationDetailsByIdAsync(idGuid) :
                         await _cipherRepository.GetByIdAsync(idGuid, userId);

            if (cipher == null || (request.AdminRequest && (!cipher.OrganizationId.HasValue ||
                                                            !await _currentContext.EditAnyCollection(cipher.OrganizationId.Value))))
            {
                throw new NotFoundException();
            }

            if (request.FileSize > CipherService.MAX_FILE_SIZE)
            {
                throw new BadRequestException($"Max file size is {CipherService.MAX_FILE_SIZE_READABLE}.");
            }

            var(attachmentId, uploadUrl) = await _cipherService.CreateAttachmentForDelayedUploadAsync(cipher,
                                                                                                      request.Key, request.FileName, request.FileSize, request.AdminRequest, userId);

            return(new AttachmentUploadDataResponseModel
            {
                AttachmentId = attachmentId,
                Url = uploadUrl,
                FileUploadType = _attachmentStorageService.FileUploadType,
                CipherResponse = request.AdminRequest ? null : new CipherResponseModel((CipherDetails)cipher, _globalSettings),
                CipherMiniResponse = request.AdminRequest ? new CipherMiniResponseModel(cipher, _globalSettings, cipher.OrganizationUseTotp) : null,
            });
        }
Esempio n. 2
0
        public async Task <(string attachmentId, string uploadUrl)> CreateAttachmentForDelayedUploadAsync(Cipher cipher,
                                                                                                          AttachmentRequestModel request, Guid savingUserId)
        {
            await ValidateCipherEditForAttachmentAsync(cipher, savingUserId, request.AdminRequest, request.FileSize);

            var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
            var data         = new CipherAttachment.MetaData
            {
                AttachmentId = attachmentId,
                FileName     = request.FileName,
                Key          = request.Key,
                Size         = request.FileSize,
                Validated    = false,
            };

            var uploadUrl = await _attachmentStorageService.GetAttachmentUploadUrlAsync(cipher, data);

            await _cipherRepository.UpdateAttachmentAsync(new CipherAttachment
            {
                Id             = cipher.Id,
                UserId         = cipher.UserId,
                OrganizationId = cipher.OrganizationId,
                AttachmentId   = attachmentId,
                AttachmentData = JsonConvert.SerializeObject(data)
            });

            cipher.AddAttachment(attachmentId, data);
            await _pushService.PushSyncCipherUpdateAsync(cipher, null);

            return(attachmentId, uploadUrl);
        }
        public async Task <IActionResult> Attachment([FromBody] AttachmentRequestModel model)
        {
            var response = new APIResponse <AttachmentResponseModel>()
            {
                Success = true
            };
            AttachmentResponseModel responseModel = new AttachmentResponseModel();

            try
            {
                if (ModelState.IsValid)
                {
                    var check = await _accountCtx.Citations.ForAccount(CommonAccount.Id).AsNoTracking().AnyAsync(x => x.Id == model.CitationId);

                    if (check)
                    {
                        foreach (var attachment in model.Attachments)
                        {
                            // put dude in the database
                            Attachment attach = new Attachment()
                            {
                                AccountId      = CommonAccount.Id,
                                ContentLength  = attachment.Length,
                                MimeType       = attachment.MimeType,
                                FileName       = attachment.FileName,
                                Description    = attachment.Description,
                                Key            = attachment.Key,
                                Duration       = attachment.Duration,
                                AttachmentType = attachment.FileName.GetAttachmentType(),
                                CreateUserId   = User.GetJWTLoggedInUserId().Value,
                                UpdateUserId   = User.GetJWTLoggedInUserId().Value
                            };
                            var citationAttach = await SaveAttachments(attach, model.CitationId);

                            var obj = Mapper.Map <AttachmentResponse>(citationAttach);
                            responseModel.Response.Add(obj);
                        }
                        //Create Receipt
                        await _citationSvc.CreateCitationReceipt(CommonAccount.Id, model.CitationId, model.DeviceReceipt, model.DevicePublicKey, User.GetJWTLoggedInUserId().Value);

                        response.Data = responseModel;
                    }
                    else
                    {
                        response.Success = false;
                        response.Errors.Add(new Error {
                            Code = 0, Message = "Invalid Citation Id "
                        });
                    }
                }
                else
                {
                    response.Success = false;
                    response.Errors.AddRange(ModelState.ToErrors());
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return(Ok(response));
        }