-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTraffiti.aspx.cs
263 lines (234 loc) · 10.1 KB
/
AddTraffiti.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
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class AddTraffiti : System.Web.UI.Page
{
string accessKey = ConfigurationSettings.AppSettings["AWSAccessKey"];
string secretKey = ConfigurationSettings.AppSettings["AWSSecretKey"];
protected void Page_Load(object sender, EventArgs e)
{
if (Session["author_id"] == null)
{
Response.Redirect("Login.aspx");
}
if (IsPostBack && imagesUploader.PostedFile != null)
{
uploadTemp();
ShowCurrentTemp();
}
}
public void uploadTemp()
{
if (imagesUploader.HasFile)
{
//client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
string fileName = userPostedFile.FileName;
string fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - (fileName.LastIndexOf(".") + 1));
if (userPostedFile.ContentLength > 0 && userPostedFile.ContentLength < 2048000 && (fileExt.ToLower() == "png" || fileExt.ToLower() == "jpeg" || fileExt.ToLower() == "jpg"))
{
byte[] fileData = null;
Stream fileStream = null;
int length = 0;
length = userPostedFile.ContentLength;
fileData = new byte[length + 1];
fileStream = userPostedFile.InputStream;
fileStream.Read(fileData, 0, length);
MemoryStream stream = new MemoryStream(fileData);
UploadImageS3("client_temp/" + Session["author_id"] + "/" + fileName, stream);
ApiClass apiClass = new ApiClass();
string url = "http://api.traffiti.co/api/Wall/SaveTempPhoto";
string json = "{'authorID': " + Session["author_id"] + ", 'photoPath': '" + "client_temp/" + Session["author_id"] + "/" + fileName + "'}";
string result = apiClass.PostCallApi(url, json);
}
}
catch (Exception ex)
{
}
}
}
}
public class TempPhoto
{
public int id { set; get; }
public string photo_path { set; get; }
}
List<TempPhoto> tempPhotoList = new List<TempPhoto>();
public void ShowCurrentTemp()
{
ApiClass apiClass = new ApiClass();
string url = "http://api.traffiti.co/api/Wall/GetTempPhoto";
string json = "{'authorID': " + Session["author_id"] + "}";
string result = apiClass.PostCallApi(url, json);
JavaScriptSerializer js = new JavaScriptSerializer();
tempPhotoList = js.Deserialize<List<TempPhoto>>(result);
for (int i = 0; i < tempPhotoList.Count; i++)
{
tempPhotoList[i].photo_path = ConfigurationSettings.AppSettings["imageDomain"] + tempPhotoList[i].photo_path;
}
tempImageRepeater.DataSource = tempPhotoList;
tempImageRepeater.DataBind();
}
protected void postBtn_Click(object sender, EventArgs e)
{
try
{
if (Session["author_id"] == null)
{
content.Text = "Empty session";
}
else
{
ShowCurrentTemp();
ApiClass apiClass = new ApiClass();
string url = "http://api.traffiti.co/api/Wall/CreateWall";
ComingCreateWall ccw = new ComingCreateWall();
ccw.author_id = Convert.ToInt32(Session["author_id"]);
if (!string.IsNullOrEmpty(hiddenLocation.Value))
{
ccw.location = hiddenLocation.Value;
}
else
{
ccw.location = "";
}
if (!string.IsNullOrEmpty(lat.Value))
{
ccw.lat = lat.Value;
}
else
{
ccw.lat = "";
}
if (!string.IsNullOrEmpty(lat.Value))
{
ccw.lon = lon.Value;
}
else
{
ccw.lon = "";
}
ccw.message = content.Text;
ccw.photoList = new List<string>();
for (int i = 0; i < tempPhotoList.Count; i++)
{
ccw.photoList.Add(tempPhotoList[i].photo_path);
}
ccw.publishList = new List<string>();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(ccw);
string result = apiClass.PostCallApi(url, json);
ComingCreateWall afterCreateWall = js.Deserialize<ComingCreateWall>(result);
for (int i = 0; i < tempPhotoList.Count; i++)
{
//copy photo
WebClient WC = new WebClient();
MemoryStream stream = new MemoryStream(WC.DownloadData(tempPhotoList[i].photo_path));
UploadImageS3("client_upload/" + Session["author_id"] + "/" + afterCreateWall.publishList[i], stream);
}
}
//Response.Redirect("Recently.aspx");
}
catch (Exception ex)
{
content.Text = ex.Message;
}
Response.Redirect("RecentlyList.aspx");
//if (imagesUploader.HasFile)
//{
// //client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);
// HttpFileCollection uploadedFiles = Request.Files;
// for(int i = 0;i < uploadedFiles.Count;i++)
// {
// HttpPostedFile userPostedFile = uploadedFiles[i];
// try
// {
// string fileName = userPostedFile.FileName;
// string fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - (fileName.LastIndexOf(".") + 1));
// if (userPostedFile.ContentLength > 0 && userPostedFile.ContentLength < 2048000 && (fileExt.ToLower() == "png" || fileExt.ToLower() == "jpeg" || fileExt.ToLower() == "jpg"))
// {
// byte[] fileData = null;
// Stream fileStream = null;
// int length = 0;
// length = userPostedFile.ContentLength;
// fileData = new byte[length + 1];
// fileStream = userPostedFile.InputStream;
// fileStream.Read(fileData, 0, length);
// MemoryStream stream = new MemoryStream(fileData);
// UploadImageS3("client_temp/" + Session["author_id"] + "/" + fileName, stream);
// }
// }
// catch (Exception ex)
// {
// }
// }
//}
}
/*function geoTest() {
alert(google.loader.ClientLocation);
if (google.loader.ClientLocation) {
var latitude = google.loader.ClientLocation.latitude;
var longitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var country = google.loader.ClientLocation.address.country;
var country_code = google.loader.ClientLocation.address.country_code;
var region = google.loader.ClientLocation.address.region;
document.getElementById('<%=locationText.ClientID%>').innerHTML = city;
}
}
geoTest();*/
// <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCDLnmGm_kFXPikv3hzOjYTVYNcqGQnsF&language=zh&callback=initMap">
//</script>
//<script type="text/javascript" src="http://www.google.com/jsapi"></script>
//<script>
// google.load("language", "zh");
// </script>
private void UploadImageS3(string fileName, MemoryStream fileStream)
{
try
{
AmazonS3 client;
AmazonS3Config config = new AmazonS3Config().WithCommunicationProtocol(Protocol.HTTP);
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, config))
{
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName("traffiti");
request.WithCannedACL(S3CannedACL.PublicRead);
request.WithKey(fileName).InputStream = fileStream;
request.AddHeaders(Amazon.S3.Util.AmazonS3Util.CreateHeaderEntry("Cache-Control", "max-age=31536000"));
S3Response response = client.PutObject(request);
}
}
catch (AmazonS3Exception amazonS3Exception)
{
if (amazonS3Exception.ErrorCode != null &&
(amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
||
amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
{
Console.WriteLine("Check the provided AWS Credentials.");
Console.WriteLine("For service sign up go to http://aws.amazon.com/s3");
}
else
{
Console.WriteLine(
"Error occurred. Message:'{0}' when writing an object"
, amazonS3Exception.Message);
}
}
}
}