Пример #1
0
        public async Task <IEnumerable <Vm> > GetByTeamIdAsync(Guid teamId, string name, bool includePersonal, bool onlyMine, CancellationToken ct)
        {
            if (!(await _playerService.CanAccessTeamAsync(teamId, ct)))
            {
                throw new ForbiddenException();
            }

            var vmQuery = _context.VmTeams
                          .Where(v => v.TeamId == teamId)
                          .Select(v => v.Vm)
                          .Distinct();

            if (onlyMine)
            {
                vmQuery = vmQuery.Where(v => v.UserId.HasValue && v.UserId == _user.GetId());
            }
            else if (!includePersonal)
            {
                vmQuery = vmQuery.Where(v => !v.UserId.HasValue);
            }

            if (!string.IsNullOrEmpty(name))
            {
                vmQuery = vmQuery.Where(v => v.Name == name);
            }

            // order the vms by name honoring trailing number as a number (i.e. abc1, abc2, abc10, abc11)
            var vmList = sortVmsByNumber(await vmQuery.ToListAsync(ct));

            if (includePersonal && !onlyMine)
            {
                var personalVms = vmList.Where(v => v.UserId.HasValue).ToList();

                if (personalVms.Any())
                {
                    if (!(await _playerService.CanManageTeamAsync(teamId, ct)))
                    {
                        foreach (var userVm in personalVms)
                        {
                            if (userVm.UserId.Value != _user.GetId())
                            {
                                vmList.Remove(userVm);
                            }
                        }
                    }
                }
            }

            return(_mapper.Map <IEnumerable <Vm> >(vmList));
        }
Пример #2
0
        public async Task <IActionResult> Upload(Guid uuid)
        {
            var formFile = Request.Form.Files[0];
            var filename = SanitizeFilename(formFile.Name);
            var scope    = Request.Form["scope"][0];
            var size     = Convert.ToInt64(Request.Form["size"][0]);

            if (size > _isoUploadOptions.MaxFileSize)
            {
                throw new Exception($"File exceeds the {_isoUploadOptions.MaxFileSize} byte maximum size.");
            }

            var teamId = await _playerService.GetPrimaryTeamByExerciseIdAsync(uuid, new System.Threading.CancellationToken());

            if (scope == "exercise")
            {
                if (!(await _playerService.CanManageTeamAsync(teamId, new System.Threading.CancellationToken())))
                {
                    throw new InvalidOperationException("You do not have permission to upload public files for this Exercise");
                }
            }

            var destPath = Path.Combine(
                _isoUploadOptions.BasePath,
                uuid.ToString(),
                (scope == "exercise") ? uuid.ToString() : teamId.ToString()
                );

            var destFile = Path.Combine(destPath, filename);

            Directory.CreateDirectory(destPath);

            using (var sourceStream = formFile.OpenReadStream())
            {
                if (filename.ToLower().EndsWith(".iso"))
                {
                    using (var destStream = System.IO.File.Create(destFile))
                    {
                        await sourceStream.CopyToAsync(destStream);
                    }
                }
                else
                {
                    CDBuilder builder = new CDBuilder();
                    builder.UseJoliet        = true;
                    builder.VolumeIdentifier = "PlayerIso";
                    builder.AddFile(filename, sourceStream);
                    builder.Build(destFile + ".iso");
                }
            }

            return(Json("ISO was uploaded"));
        }