public Task <object> Get(GetDownload request)
        {
            var item = _libraryManager.GetItemById(request.Id);
            var auth = _authContext.GetAuthorizationInfo(Request);

            var user = _userManager.GetUserById(auth.UserId);

            if (user != null)
            {
                if (!item.CanDownload(user))
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }
            else
            {
                if (!item.CanDownload())
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }

            var headers = new Dictionary <string, string>();

            if (user != null)
            {
                LogDownload(item, user, auth);
            }

            return(ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
            {
                Path = item.Path,
                ResponseHeaders = headers
            }));
        }
        public Task <object> Get(GetDownload request)
        {
            var item = _libraryManager.GetItemById(request.Id);
            var auth = _authContext.GetAuthorizationInfo(Request);

            var user = auth.User;

            if (user != null)
            {
                if (!item.CanDownload(user))
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }
            else
            {
                if (!item.CanDownload())
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }

            var headers = new Dictionary <string, string>();

            if (user != null)
            {
                LogDownload(item, user, auth);
            }

            var path = item.Path;

            // Quotes are valid in linux. They'll possibly cause issues here
            var filename = (Path.GetFileName(path) ?? string.Empty).Replace("\"", string.Empty);

            if (!string.IsNullOrWhiteSpace(filename))
            {
                // Kestrel doesn't support non-ASCII characters in headers
                if (Regex.IsMatch(filename, "[^[:ascii:]]"))
                {
                    // Manually encoding non-ASCII characters, following https://tools.ietf.org/html/rfc5987#section-3.2.2
                    headers[HeaderNames.ContentDisposition] = "attachment; filename*=UTF-8''" + WebUtility.UrlEncode(filename);
                }
                else
                {
                    headers[HeaderNames.ContentDisposition] = "attachment; filename=\"" + filename + "\"";
                }
            }

            return(ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
            {
                Path = path,
                ResponseHeaders = headers
            }));
        }
Exemple #3
0
        public Task <object> Get(GetDownload request)
        {
            var item = _libraryManager.GetItemById(request.Id);
            var auth = _authContext.GetAuthorizationInfo(Request);

            var user = auth.User;

            if (user != null)
            {
                if (!item.CanDownload(user))
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }
            else
            {
                if (!item.CanDownload())
                {
                    throw new ArgumentException("Item does not support downloading");
                }
            }

            var headers = new Dictionary <string, string>();

            if (user != null)
            {
                LogDownload(item, user, auth);
            }

            var path = item.Path;

            // Quotes are valid in linux. They'll possibly cause issues here
            var filename = (Path.GetFileName(path) ?? string.Empty).Replace("\"", string.Empty);

            if (!string.IsNullOrWhiteSpace(filename))
            {
                headers["Content-Disposition"] = "attachment; filename=\"" + filename + "\"";
            }

            return(ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
            {
                Path = path,
                ResponseHeaders = headers
            }));
        }