示例#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 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;
            }
        }