Пример #1
0
        public async Task <IActionResult> RemoveTeamPhoto(long id)
        {
            if (!(await _authorizationService.AuthorizeAsync(User, new TeamEntity(id),
                                                             Authorization.TeamOperations.ChangePhoto)).Succeeded)
            {
                return(Forbid());
            }

            var photoFile = new TeamPhotoStaticFile(_webHostingEnvironment, _siteContext,
                                                    _loggerFactory.CreateLogger <TeamPhotoStaticFile>());

            photoFile.DeleteMostRecentFile(id);

            return(RedirectToAction(nameof(TeamPhoto), nameof(Upload), new { Organization = _siteContext.UrlSegmentValue, id }));
        }
Пример #2
0
        public async Task <IActionResult> TeamPhoto(long id, CancellationToken cancellationToken)
        {
            if (!(await _authorizationService.AuthorizeAsync(User, new TeamEntity(id),
                                                             Authorization.TeamOperations.ChangePhoto)).Succeeded)
            {
                return(Forbid());
            }

            var team = await _siteContext.AppDb.TeamRepository.GetTeamEntityAsync(
                new PredicateExpression(TeamFields.Id == id), cancellationToken);

            if (team == null)
            {
                return(NotFound());
            }

            var teamPhoto = new TeamPhotoStaticFile(_webHostingEnvironment, _siteContext,
                                                    _loggerFactory.CreateLogger <TeamPhotoStaticFile>());

            var model = new TeamPhotoViewModel
            {
                Team = team
            };

            var fi = teamPhoto.GetUriInfo(id);

            if (fi.Uri != null)
            {
                model.PhotoFileUrl  = fi.Uri;
                model.PhotoFileDate = fi.Date;
            }
            else
            {
                // shouldn't be the case
                _logger.LogError("Photo file for team id '{0}' not found", id);
                model.PhotoFileUrl = null;
            }

            return(View(ViewNames.Upload.TeamPhoto, model));
        }
Пример #3
0
        public async Task <IActionResult> TeamPhoto([FromForm] IFormFile file, [FromForm] long teamId, CancellationToken cancellationToken)
        {
            if (!(await _authorizationService.AuthorizeAsync(User, new TeamEntity(teamId),
                                                             Authorization.TeamOperations.ChangePhoto)).Succeeded)
            {
                return(Forbid());
            }

            var teamInfo =
                await _siteContext.AppDb.TeamRepository.GetTeamEntityAsync(
                    new PredicateExpression(TeamFields.Id == teamId), cancellationToken);

            if (teamInfo == null)
            {
                return(NotFound());
            }

            // IIS will return a 404.13 HTTP status code when the uploaded file is bigger than allowed.
            // Set <requestLimits maxAllowedContentLength="1000000000" /> (here: 1GB) in web.config to increase,
            // or set Microsoft.AspNetCore.Http.Features.FormOptions in Startup.
            if (file.Length <= 0)
            {
                Response.StatusCode = 409;
                return(Json(new { error = _localizer["Uploaded file is empty"].Value }));
            }

            if (file.Length > 5000000)
            {
                Response.StatusCode = 409;
                return(Json(new { error = _localizer["Maximum file size is 5 MB"].Value }));
            }

            var photoFile = new TeamPhotoStaticFile(_webHostingEnvironment, _siteContext,
                                                    _loggerFactory.CreateLogger <TeamPhotoStaticFile>());

            var extension =
                Path.GetExtension(ContentDispositionHeaderValue.Parse(file.ContentDisposition)?.FileName.Value
                                  ?.ToLowerInvariant());

            if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(extension ?? string.Empty))
            {
                Response.StatusCode = 409;
                var msg = _localizer["Uploaded file must be of type JPG, JPEG or PNG"].Value;
                _logger.LogError(msg);
                return(Json(new { error = msg }));
            }

            try
            {
                var savedFilename =
                    await photoFile.SaveFileAsync(file, extension, teamId, true, cancellationToken);

                if (photoFile.GetFileInfo(teamId).Filename != savedFilename)
                {
                    throw new Exception("Saved filename could not be found");
                }
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, "File for team id '{0}' could not be stored", teamId);
                Response.StatusCode = 409;
                return(Json(new { error = _localizer["Uploaded file could not be processed"].Value }));
            }

            return(Json(new
            {
                info = _localizer["Upload completed"].Value, imageUrl = Url.Content(photoFile.GetUriInfo(teamId).Uri)
            }));
        }