-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetUserImg.ashx
150 lines (147 loc) · 3.78 KB
/
getUserImg.ashx
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
<%@ WebHandler Language="C#" Class="getUserImg" %>
using System;
using System.Web;
using System.Drawing;
using System.IO;
public class getUserImg : IHttpHandler
{
void error(HttpContext context)
{
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddHours(24));
context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0));
context.Response.ContentType = "image/png";
string file = context.Server.MapPath("images/img_avatar.png");
context.Response.AddHeader("Content-Length", (new FileInfo(file)).Length.ToString());
context.Response.WriteFile(file);
}
public void ProcessRequest(HttpContext context)
{
object sz = context.Request.QueryString["sz"];
string sid = context.Request.QueryString["id"];
if (string.IsNullOrEmpty(sid))
{
string sttid = context.Request.QueryString["ttid"];
if (string.IsNullOrEmpty(sttid))
{
string eml = context.Request.QueryString["eml"];
if (!string.IsNullOrEmpty(eml))
{
DefectUser du = DefectUser.FindByEmail(eml);
if (du == null)
{
error(context);
return;
}
sid = du.TRID.ToString();
}
if (string.IsNullOrEmpty(sid))
{
string pho = context.Request.QueryString["pho"];
if (!string.IsNullOrEmpty(pho))
{
MPSUser mu = MPSUser.FindUserbyPhone(pho);
if (mu != null)
{
sid = mu.ID.ToString();
}
}
}
if (string.IsNullOrEmpty(sid))
{
error(context);
return;
}
}
else
{
int ttid;
if (!int.TryParse(sttid, out ttid) || ttid < 1)
{
error(context);
return;
}
DefectUser du = new DefectUser(ttid);
sid = du.TRID.ToString();
}
}
int id;
if (!int.TryParse(sid, out id) || id < 1)
{
error(context);
return;
}
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddDays(10));
context.Response.Cache.SetMaxAge(new TimeSpan(10, 0, 0, 0));
context.Response.ContentType = "image/jpg";
int? isz = null;
if (sz != null)
{
isz = Convert.ToInt32(sz);
};
LoadImageFile(context, id, isz);
}
public bool IsReusable
{
get
{
return false;
}
}
static object _Lock = new object();
void DellOldFiles(string dir, string validprefix)
{
var files = Directory.EnumerateFiles(dir);
foreach (var file in files)
{
string filename = Path.GetFileName(file);
if (!filename.StartsWith(validprefix) && !filename.ToUpper().Contains("GITIGNORE"))
{
File.Delete(file);
}
}
}
void LoadImageFile(HttpContext context, int id, int? size)
{
string dir = context.Server.MapPath($"images/cache/users/");
string prefix = ReferenceVersion.REFSVERSION();
string newfilename = $"{dir}{prefix}scaled-id-{size}-sz-{id}.jpg";
string origfilename = $"{dir}{prefix}orig-id-{id}.jpg";
if (!File.Exists(origfilename) || !File.Exists(newfilename))
{
lock (_Lock)
{
if (!File.Exists(origfilename))
{
MPSUser u = new MPSUser(id);
byte[] data = u.GetImage();
if (data == null)
{
error(context);
return;
}
File.WriteAllBytes(origfilename, data);
DellOldFiles(dir, prefix);
}
if (size == null)
{
File.Copy(origfilename, newfilename);
}
else
{
using (Bitmap orig = new Bitmap(origfilename))
{
using (Bitmap newb = new Bitmap((Image)orig, new Size(size.Value, size.Value)))
{
newb.Save(newfilename);
DellOldFiles(dir, prefix);
}
}
}
}
}
context.Response.AddHeader("Content-Length", (new FileInfo(newfilename)).Length.ToString());
context.Response.TransmitFile(newfilename);
}
}