-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditSalutePhoto.aspx.cs
300 lines (257 loc) · 10.7 KB
/
EditSalutePhoto.aspx.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using AspNetDating.Classes;
namespace AspNetDating
{
public partial class EditSalutePhoto : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadStrings();
LoadPhoto();
}
}
private void LoadStrings()
{
if (!Config.Photos.EnableSalutePhoto) Response.Redirect("~/Default.aspx");
LargeBoxStart1.Title = "Upload Salute Photo".Translate();
SmallBoxStart1.Title = Lang.Trans("Profile Management");
lblPhoto.Text = Lang.Trans("Photo");
chkPrivatePhoto.Text = Lang.Trans("set as private");
if (!Config.Photos.EnablePrivatePhotos
||
Config.CommunityFaceControlSystem.EnableCommunityFaceControl && !CurrentUserSession.FaceControlApproved)
chkPrivatePhoto.Visible = false;
lnkSave.Text = Lang.Trans("Save");
lnkCancel.Text = Lang.Trans("Cancel");
}
private void LoadPhoto()
{
Photo salutePhoto = Photo.FetchSalute(CurrentUserSession.Username);
if (salutePhoto != null)
{
if (salutePhoto.User.Username == CurrentUserSession.Username)
{
string tempFileName;
if (!Misc.GetTempFileName(out tempFileName))
tempFileName = Path.GetTempFileName();
salutePhoto.Image.Save(tempFileName);
salutePhoto.Image = null;
Session["temp_photo"] = salutePhoto;
Session["temp_photo_fileName"] = tempFileName;
txtName.Text = salutePhoto.Name;
txtDescription.Text = salutePhoto.Description;
chkPrivatePhoto.Checked = salutePhoto.PrivatePhoto;
if (salutePhoto.ExplicitPhoto)
chkPrivatePhoto.Enabled = false;
divImageRotateControls.Visible = true;
}
else return;
}
else
{
Session["temp_photo"] = null;
Session["temp_photo_fileName"] = null;
txtName.Text = "";
txtDescription.Text = "";
chkPrivatePhoto.Checked = false;
lblPhoto.Visible = true;
ufPhoto.Visible = true;
btnUpload.Visible = true;
divImageRotateControls.Visible = false;
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
Image image;
try
{
image = Image.FromStream
(ufPhoto.PostedFile.InputStream);
}
catch
{
lblError.Text = Lang.Trans("Invalid image!");
return;
}
Photo photo;
if (Session["temp_photo"] == null)
photo = new Photo();
else
photo = (Photo)Session["temp_photo"];
if (image.Height < Config.Photos.PhotoMinHeight
|| image.Width < Config.Photos.PhotoMinWidth)
{
lblError.Text = Lang.Trans("The photo is too small!");
return;
}
photo.Image = image;
string tempFileName;
if (!Misc.GetTempFileName(out tempFileName))
tempFileName = Path.GetTempFileName();
photo.Image.Save(tempFileName);
Session["temp_photo_fileName"] = tempFileName;
if (Config.Photos.AutoApprovePhotos
|| CurrentUserSession.BillingPlanOptions.AutoApprovePhotos.Value
|| CurrentUserSession.Level != null && CurrentUserSession.Level.Restrictions.AutoApprovePhotos)
{
photo.Approved = true;
}
else
{
photo.Approved = false;
}
photo.ExplicitPhoto = false;
chkPrivatePhoto.Enabled = true;
photo.Image = null;
Session["temp_photo"] = photo;
divImageRotateControls.Visible = true;
}
protected void btnRotateLeft_Click(object sender, EventArgs e)
{
var tempPhotoFile = (string)Session["temp_photo_fileName"];
if (string.IsNullOrEmpty(tempPhotoFile)) return;
using (Image img = Image.FromFile(tempPhotoFile))
{
img.RotateFlip(RotateFlipType.Rotate270FlipNone);
string newTempPhotoFile;
if (!Misc.GetTempFileName(out newTempPhotoFile))
newTempPhotoFile = Path.GetTempFileName();
img.Save(newTempPhotoFile);
Session["temp_photo_fileName"] = newTempPhotoFile;
}
try
{
File.Delete(tempPhotoFile);
}
catch (IOException)
{
}
}
protected void btnRotateRight_Click(object sender, EventArgs e)
{
var tempPhotoFile = (string)Session["temp_photo_fileName"];
if (string.IsNullOrEmpty(tempPhotoFile)) return;
using (Image img = Image.FromFile(tempPhotoFile))
{
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
string newTempPhotoFile;
if (!Misc.GetTempFileName(out newTempPhotoFile))
newTempPhotoFile = Path.GetTempFileName();
img.Save(newTempPhotoFile);
Session["temp_photo_fileName"] = newTempPhotoFile;
}
try
{
File.Delete(tempPhotoFile);
}
catch (IOException)
{
}
}
protected void lnkSave_Click(object sender, EventArgs e)
{
var photo = (Photo)Session["temp_photo"];
if (photo == null)
{
if (ufPhoto.HasFile)
{
btnUpload_Click(null, null);
photo = (Photo)Session["temp_photo"];
}
else
{
lblError.Text = Lang.Trans("Please upload image first!");
return;
}
}
// ReSharper disable AssignNullToNotNullAttribute
photo.Image = Image.FromFile(Session["temp_photo_fileName"] as string);
// ReSharper restore AssignNullToNotNullAttribute
if (photo.Id > 0)
{
try
{
string cacheFileDir = Config.Directories.ImagesCacheDirectory + "/" + photo.Id % 10;
string cacheFileMask = String.Format("photo_{0}_*.jpg", photo.Id);
foreach (string file in Directory.GetFiles(cacheFileDir, cacheFileMask))
{
File.Delete(file);
}
cacheFileMask = String.Format("photoface_{0}_*.jpg", photo.Id);
foreach (string file in Directory.GetFiles(cacheFileDir, cacheFileMask))
{
File.Delete(file);
}
}
catch (Exception err)
{
Global.Logger.LogError(err);
}
}
photo.User = CurrentUserSession;
photo.Name = Config.Misc.EnableBadWordsFilterProfile ? Parsers.ProcessBadWords(txtName.Text) : txtName.Text;
photo.Description = Config.Misc.EnableBadWordsFilterProfile
? Parsers.ProcessBadWords(txtDescription.Text)
: txtDescription.Text;
photo.PrivatePhoto = chkPrivatePhoto.Checked;
photo.Salute = true;
bool isNewPhoto = photo.Id == 0;
photo.Save(true);
if (photo.Approved && !photo.PrivatePhoto && isNewPhoto)
{
#region Add NewFriendPhoto Event
Event newEvent = new Event(CurrentUserSession.Username);
newEvent.Type = Event.eType.NewFriendPhoto;
NewFriendPhoto newFriendPhoto = new NewFriendPhoto();
newFriendPhoto.PhotoID = photo.Id;
newEvent.DetailsXML = Misc.ToXml(newFriendPhoto);
newEvent.Save();
string[] usernames = Classes.User.FetchMutuallyFriends(CurrentUserSession.Username);
foreach (string friendUsername in usernames)
{
if (Config.Users.NewEventNotification)
{
string text = String.Format("Your friend {0} has uploaded a new photo".Translate(),
"<b>" + photo.Username + "</b>");
string thumbnailUrl = ImageHandler.CreateImageUrl(photo.Id, 50, 50, false, true, true);
Classes.User.SendOnlineEventNotification(photo.Username, friendUsername,
text, thumbnailUrl,
UrlRewrite.CreateShowUserPhotosUrl(photo.Username));
}
}
#endregion
}
photo.Image.Dispose();
try
{
if (Session["temp_photo_fileName"] != null)
File.Delete((string)Session["temp_photo_fileName"]);
}
catch (IOException)
{
}
Session["temp_photo"] = null;
Session["temp_photo_fileName"] = null;
Response.Redirect("~/Profile.aspx?sel=photos");
}
/// <summary>
/// Handles the Click event of the lnkCancel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void lnkCancel_Click(object sender, EventArgs e)
{
Session["temp_photo"] = null;
Session["temp_photo_fileName"] = null;
Response.Redirect("~/Profile.aspx?sel=photos");
}
}
}