public async Task <DownloadedFileResult> Handle(GetDownloadByIdQuery request, CancellationToken cancellationToken)
        {
            DownloadedFileResult Result = new DownloadedFileResult();

            var dResult = await _unitOfWork.Downloads.GetDownloadedFile(request.DownloadId);

            //Check download exist
            if (dResult.Download == null || dResult.File == null)
            {
                Result.ErrorContent = new ErrorContent("No file with the provided id was found", ErrorOrigin.Client);
                return(Result);
            }
            // Check if it's the same requester
            if (!request.RequesterAddress.Contains(dResult.Download.IpAdress))
            {
                Result.ErrorContent = new ErrorContent("Hotlinking disabled by the administrator.", ErrorOrigin.Client);
                return(Result);
            }

            // Get the download options
            DownloadOptionsResult options = await _mediatr.Send(new GetDownloadOptionsQuery(_caller.Identity.IsAuthenticated));

            if (options.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent("Error while reading download options", ErrorOrigin.Server);
                return(Result);
            }

            // All good, map results
            Result.File          = dResult.File;
            Result.Download      = dResult.Download;
            Result.DownloadSpeed = options.Speed;

            return(Result);
        }
        public async Task <RequestDownloadResult> Handle(RequestDownloadQuery request, CancellationToken cancellationToken)
        {
            string userId = _caller.GetUserId();

            RequestDownloadResult Result = new RequestDownloadResult()
            {
                File = await _unitOfWork.Files.FirstOrDefaultAsync(s => s.Id == request.FileId && s.Status != ItemStatus.To_Be_Deleted)
            };

            // Check if file exist
            if (Result.File == null)
            {
                Result.ErrorContent = new ErrorContent("No file with the provided id was found", ErrorOrigin.Client);
                return(Result);
            }
            // Check file availablability
            if (userId != Result.File.UserId && Result.File.Status != ItemStatus.Visible)
            {
                Result.ErrorContent = new ErrorContent("This file is not available for public downloads", ErrorOrigin.Client);
                return(Result);
            }
            // Get the download options
            DownloadOptionsResult options = await _mediatr.Send(new GetDownloadOptionsQuery(_caller.Identity.IsAuthenticated));

            if (options.State != OperationState.Success)
            {
                Result.ErrorContent = new ErrorContent("Error while reading download options", ErrorOrigin.Client);
                return(Result);
            }

            // Check if there is already a download in progress and get remaining time
            var currentDownloads = await GetTTW(options.TTW);

            Result.WaitTime = currentDownloads.TotalDownloads > 0
                              ? currentDownloads.TimeToWait
                              : options.WaitTime;

            return(Result);
        }