summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/src/System.Web.WebPages/DefaultDisplayMode.cs
blob: ee0e0faeb32b7ee93ed0be9a50b8f1031a90d8f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.IO;

namespace System.Web.WebPages
{
    /// <summary>
    /// The <see cref="DefaultDisplayMode"/> can take any suffix and determine if there is a corresponding
    /// file that exists given a path and request by transforming the path to contain the suffix.
    /// Add a new DefaultDisplayMode to the Modes collection to handle a new suffix or inherit from
    /// DefaultDisplayMode to provide custom logic to transform paths with a suffix.
    /// </summary>
    public class DefaultDisplayMode : IDisplayMode
    {
        private readonly string _suffix;

        public DefaultDisplayMode()
            : this(DisplayModeProvider.DefaultDisplayModeId)
        {
        }

        public DefaultDisplayMode(string suffix)
        {
            _suffix = suffix ?? String.Empty;
        }

        /// <summary>
        /// When set, the <see cref="DefaultDisplayMode"/> will only be available to return Display Info for a request
        /// if the ContextCondition evaluates to true.
        /// </summary>
        public Func<HttpContextBase, bool> ContextCondition { get; set; }

        public virtual string DisplayModeId
        {
            get { return _suffix; }
        }

        public bool CanHandleContext(HttpContextBase httpContext)
        {
            return ContextCondition == null || ContextCondition(httpContext);
        }

        /// <summary>
        /// Returns DisplayInfo with the transformed path if it exists.
        /// </summary>
        public virtual DisplayInfo GetDisplayInfo(HttpContextBase httpContext, string virtualPath, Func<string, bool> virtualPathExists)
        {
            string transformedFilename = TransformPath(virtualPath, _suffix);
            if (transformedFilename != null && virtualPathExists(transformedFilename))
            {
                return new DisplayInfo(transformedFilename, this);
            }

            return null;
        }

        /// <summary>
        /// Transforms paths according to the following rules:
        /// \some\path.blah\file.txt.zip -> \some\path.blah\file.txt.suffix.zip
        /// \some\path.blah\file -> \some\path.blah\file.suffix
        /// </summary>
        protected virtual string TransformPath(string virtualPath, string suffix)
        {
            if (String.IsNullOrEmpty(suffix))
            {
                return virtualPath;
            }

            string extension = Path.GetExtension(virtualPath);
            return Path.ChangeExtension(virtualPath, suffix + extension);
        }
    }
}