Пример #1
0
        public async Task UploadFileToExistingSendAsync(Stream stream, Send send)
        {
            if (send?.Data == null)
            {
                throw new BadRequestException("Send does not have file data");
            }

            if (send.Type != SendType.File)
            {
                throw new BadRequestException("Not a File Type Send.");
            }

            var data = JsonSerializer.Deserialize <SendFileData>(send.Data);

            if (data.Validated)
            {
                throw new BadRequestException("File has already been uploaded.");
            }

            await _sendFileStorageService.UploadNewFileAsync(stream, send, data.Id);

            if (!await ValidateSendFile(send))
            {
                throw new BadRequestException("File received does not match expected file length.");
            }
        }
Пример #2
0
        public async Task CreateSendAsync(Send send, SendFileData data, Stream stream, long requestLength)
        {
            if (send.Type != SendType.File)
            {
                throw new BadRequestException("Send is not of type \"file\".");
            }

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

            var storageBytesRemaining = 0L;

            if (send.UserId.HasValue)
            {
                var user = await _userRepository.GetByIdAsync(send.UserId.Value);

                if (!(await _userService.CanAccessPremium(user)))
                {
                    throw new BadRequestException("You must have premium status to use file sends.");
                }

                if (user.Premium)
                {
                    storageBytesRemaining = user.StorageBytesRemaining();
                }
                else
                {
                    // Users that get access to file storage/premium from their organization get the default
                    // 1 GB max storage.
                    storageBytesRemaining = user.StorageBytesRemaining(
                        _globalSettings.SelfHosted ? (short)10240 : (short)1);
                }
            }
            else if (send.OrganizationId.HasValue)
            {
                var org = await _organizationRepository.GetByIdAsync(send.OrganizationId.Value);

                if (!org.MaxStorageGb.HasValue)
                {
                    throw new BadRequestException("This organization cannot use file sends.");
                }

                storageBytesRemaining = org.StorageBytesRemaining();
            }

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

            var fileId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
            await _sendFileStorageService.UploadNewFileAsync(stream, send, fileId);

            try
            {
                data.Id   = fileId;
                data.Size = stream.Length;
                send.Data = JsonConvert.SerializeObject(data,
                                                        new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                await SaveSendAsync(send);
            }
            catch
            {
                // Clean up since this is not transactional
                await _sendFileStorageService.DeleteFileAsync(fileId);

                throw;
            }
        }