This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGetMedia.cs
More file actions
193 lines (166 loc) · 4.58 KB
/
Copy pathGetMedia.cs
File metadata and controls
193 lines (166 loc) · 4.58 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using uComponents.Core;
using umbraco;
using umbraco.cms.businesslogic.media;
using System.Text;
namespace uComponents.Controls
{
/// <summary>
/// GetMedia server control.
/// </summary>
[DefaultProperty("MediaId")]
[ToolboxBitmap(typeof(Constants), Constants.FaviconResourcePath)]
[ToolboxData("<{0}:GetMedia runat=server MediaId=></{0}:GetMedia>")]
public class GetMedia : WebControl
{
/// <summary>
/// Gets or sets the media id.
/// </summary>
/// <value>The media id.</value>
[DefaultValue("")]
public string MediaId { get; set; }
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Controls.Add(new Literal() { Text = this.ProcessMedia(this.MediaId) });
}
/// <summary>
/// Renders the control to the specified HTML writer.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
protected override void Render(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
/// <summary>
/// Gets the HTML for file.
/// </summary>
/// <param name="media">The media.</param>
/// <returns></returns>
private string GetHtmlForFile(Media media)
{
// check that its not null
if (media != null)
{
// get the img src
var file = media.GetProperty<string>(Constants.Umbraco.Media.File);
// check that the file has a value
if (!string.IsNullOrEmpty(file))
{
// define the anchor tag
var anchor = "<a href=\"{0}\">{1}</a>";
// format the anchor tag
return string.Format(anchor, file, media.Text);
}
}
// fall-back return an empty string
return string.Empty;
}
/// <summary>
/// Gets the HTML for folder.
/// </summary>
/// <param name="media">The media.</param>
/// <returns></returns>
private string GetHtmlForFolder(Media media)
{
var html = new StringBuilder();
html.AppendFormat("<dl><dt>{0}</dt>", media.Text);
if (media.HasChildren)
{
foreach (var child in media.Children)
{
html.AppendFormat("<dd>{0}</dd>", this.ProcessMedia(child));
}
}
html.Append("</dl>");
return html.ToString();
}
/// <summary>
/// Gets the HTML for an image tag, using the Media Id.
/// </summary>
/// <param name="media">The media.</param>
/// <returns>Returns a HTML image tag from a Media Id.</returns>
private string GetHtmlForImage(Media media)
{
// check that its not null
if (media != null)
{
// get the img src
var src = media.GetProperty<string>(Constants.Umbraco.Media.File);
// check that the img src has a value
if (!string.IsNullOrEmpty(src))
{
// define the img tag
var img = "<img src=\"{0}\" alt=\"{1}\" height=\"{2}\" width=\"{3}\" />";
// get the height
var height = media.GetProperty<int>(Constants.Umbraco.Media.Height);
if (height <= 0)
{
height = (int)this.Height.Value;
}
// get the width
var width = media.GetProperty<int>(Constants.Umbraco.Media.Width);
if (width <= 0)
{
width = (int)this.Width.Value;
}
// if the default dimensions are less then zero...
if (height <= 0 && width <= 0)
{
// default the height & width to 100px;
height = 100;
width = 100;
}
// format the img tag
return string.Format(img, src, media.Text, height, width);
}
}
// fall-back return an empty string
return string.Empty;
}
/// <summary>
/// Processes the media.
/// </summary>
/// <returns></returns>
private string ProcessMedia(string mediaId)
{
if (!string.IsNullOrEmpty(mediaId))
{
return this.ProcessMedia(uQuery.GetMedia(mediaId));
}
return string.Empty;
}
/// <summary>
/// Processes the media.
/// </summary>
/// <param name="media">The media.</param>
/// <returns></returns>
private string ProcessMedia(Media media)
{
if (media != null)
{
switch (media.ContentType.Alias.ToUpper())
{
case "FILE":
return this.GetHtmlForFile(media);
case "FOLDER":
return this.GetHtmlForFolder(media);
case "IMAGE":
return this.GetHtmlForImage(media);
default:
// TODO: [LK] Implement a hook/handler for custom providers // e.g. Provider.GetHtml(media)
break;
}
}
return string.Empty;
}
}
}