コード例 #1
0
        public async Task <string> SaveFileSendAsync(Send send, SendFileData data, long fileLength)
        {
            if (send.Type != SendType.File)
            {
                throw new BadRequestException("Send is not of type \"file\".");
            }

            if (fileLength < 1)
            {
                throw new BadRequestException("No file data.");
            }

            var storageBytesRemaining = await StorageRemainingForSendAsync(send);

            if (storageBytesRemaining < fileLength)
            {
                throw new BadRequestException("Not enough storage available.");
            }

            var fileId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);

            try
            {
                data.Id        = fileId;
                data.Size      = fileLength;
                data.Validated = false;
                send.Data      = JsonConvert.SerializeObject(data,
                                                             new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                await SaveSendAsync(send);

                return(await _sendFileStorageService.GetSendFileUploadUrlAsync(send, fileId));
            }
            catch
            {
                // Clean up since this is not transactional
                await _sendFileStorageService.DeleteFileAsync(send, fileId);

                throw;
            }
        }
コード例 #2
0
        public async Task <SendFileUploadDataResponseModel> RenewFileUpload(string id, string fileId)
        {
            var userId = _userService.GetProperUserId(User).Value;
            var sendId = new Guid(id);
            var send   = await _sendRepository.GetByIdAsync(sendId);

            var fileData = JsonConvert.DeserializeObject <SendFileData>(send?.Data);

            if (send == null || send.Type != SendType.File || (send.UserId.HasValue && send.UserId.Value != userId) ||
                !send.UserId.HasValue || fileData.Id != fileId || fileData.Validated)
            {
                // Not found if Send isn't found, user doesn't have access, request is faulty,
                // or we've already validated the file. This last is to emulate create-only blob permissions for Azure
                throw new NotFoundException();
            }

            return(new SendFileUploadDataResponseModel
            {
                Url = await _sendFileStorageService.GetSendFileUploadUrlAsync(send, fileId),
                FileUploadType = _sendFileStorageService.FileUploadType,
                SendResponse = new SendResponseModel(send, _globalSettings),
            });
        }