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) // { // } // } //} }
public ComingCreateWall CreateWall(ComingCreateWall comingCreateWall) { MySqlConnection cn = new MySqlConnection(ConfigurationManager.ConnectionStrings["sq_traffiti"].ConnectionString); int wall_id = 0; int location_id = GetLocationID(comingCreateWall.location, comingCreateWall.lat, comingCreateWall.lon); try { cn.Open(); MySqlCommand createWallCmd = new MySqlCommand(@"insert into wall (author_id, created_date, like_count, fav_count, view_count, location_id, lat, lon, first_section_id) values (@author_id, NOW(), 0, 0, 0, @location_id, @lat, @lon, 0)", cn); createWallCmd.CommandType = CommandType.Text; createWallCmd.Parameters.Add("@author_id", MySqlDbType.Int32).Value = comingCreateWall.author_id; createWallCmd.Parameters.Add("@location_id", MySqlDbType.Int32).Value = location_id; createWallCmd.Parameters.Add("@lat", MySqlDbType.VarChar).Value = comingCreateWall.lat; createWallCmd.Parameters.Add("@lon", MySqlDbType.VarChar).Value = comingCreateWall.lon; createWallCmd.ExecuteNonQuery(); wall_id = Convert.ToInt32(createWallCmd.LastInsertedId); int section_id = 0; int photo_section_id = 0; string firstPath = ""; if (comingCreateWall.photoList.Count > 0) { comingCreateWall.publishList = new List <string>(); MySqlCommand createPhotoSection = new MySqlCommand(@"insert into photo_section (wall_id, display_order) values (@wall_id,1)", cn); createPhotoSection.CommandType = CommandType.Text; createPhotoSection.Parameters.Add("@wall_id", MySqlDbType.Int32).Value = wall_id; createPhotoSection.ExecuteNonQuery(); photo_section_id = Convert.ToInt32(createPhotoSection.LastInsertedId); for (int i = 0; i < comingCreateWall.photoList.Count; i++) { MySqlCommand addPhotoCmd = new MySqlCommand(@"insert into photo (photo_path, caption, display_order, photo_section, is_default, wall_id) values ('', '', @display_order, @photo_section_id, @is_default, @wall_id)", cn); addPhotoCmd.CommandType = CommandType.Text; addPhotoCmd.Parameters.Add("@photo_path", MySqlDbType.VarChar).Value = ""; addPhotoCmd.Parameters.Add("@display_order", MySqlDbType.Int32).Value = i + 1; addPhotoCmd.Parameters.Add("@photo_section_id", MySqlDbType.Int32).Value = photo_section_id; addPhotoCmd.Parameters.Add("@is_default", MySqlDbType.Int32).Value = i == 0 ? 1 : 0; addPhotoCmd.Parameters.Add("@wall_id", MySqlDbType.Int32).Value = wall_id; addPhotoCmd.ExecuteNonQuery(); int photoID = Convert.ToInt32(addPhotoCmd.LastInsertedId); string photoExtension = System.IO.Path.GetExtension(comingCreateWall.photoList[i]).ToLower(); comingCreateWall.publishList.Add(photoID + photoExtension); MySqlCommand updPhotoCmd = new MySqlCommand("update photo set photo_path = @photoPath where photo_id = @photoID", cn); updPhotoCmd.CommandType = CommandType.Text; updPhotoCmd.Parameters.Add("@photoID", MySqlDbType.Int32).Value = photoID; updPhotoCmd.Parameters.Add("@photoPath", MySqlDbType.VarChar).Value = "https://s3-ap-southeast-1.amazonaws.com/traffiti/client_upload/" + comingCreateWall.author_id + "/" + photoID + photoExtension; updPhotoCmd.ExecuteNonQuery(); if (i == 0) { firstPath = "https://s3-ap-southeast-1.amazonaws.com/traffiti/client_upload/" + comingCreateWall.author_id + "/" + photoID + photoExtension; } } } MySqlCommand createSectionCmd = new MySqlCommand(@"insert into wall_section (content, photo_section_id, display_order, wall_id) values (@content, @photo_section_id, 1, @wall_id)", cn); createSectionCmd.CommandType = CommandType.Text; createSectionCmd.Parameters.Add("@content", MySqlDbType.Text).Value = comingCreateWall.message; createSectionCmd.Parameters.Add("@photo_section_id", MySqlDbType.Int32).Value = photo_section_id; createSectionCmd.Parameters.Add("@wall_id", MySqlDbType.Int32).Value = wall_id; createSectionCmd.ExecuteNonQuery(); section_id = Convert.ToInt32(createSectionCmd.LastInsertedId); MySqlCommand updateCmd = new MySqlCommand(@"update wall set first_section_id = @section_id where wall_id = @wall_id", cn); updateCmd.Parameters.Add("@section_id", MySqlDbType.Int32).Value = section_id; updateCmd.Parameters.Add("@wall_id", MySqlDbType.Int32).Value = wall_id; updateCmd.ExecuteNonQuery(); MySqlCommand snapCmd = new MySqlCommand(@"insert into wall_snap (wall_id, content, photo_path, author_id, created_date, location_id) values (@wall_id, @content, @photo_path, @author_id, NOW(), @location_id)", cn); snapCmd.CommandType = CommandType.Text; snapCmd.Parameters.Add("@wall_id", MySqlDbType.Int32).Value = wall_id; snapCmd.Parameters.Add("@content", MySqlDbType.VarChar).Value = comingCreateWall.message; snapCmd.Parameters.Add("@photo_path", MySqlDbType.VarChar).Value = firstPath; snapCmd.Parameters.Add("@author_id", MySqlDbType.Int32).Value = comingCreateWall.author_id; snapCmd.Parameters.Add("@location_id", MySqlDbType.Int32).Value = location_id; snapCmd.ExecuteNonQuery(); MySqlCommand delTempCmd = new MySqlCommand("delete from temp_photo where author_id = @author_id", cn); delTempCmd.CommandType = CommandType.Text; delTempCmd.Parameters.Add("@author_id", MySqlDbType.Int32).Value = comingCreateWall.author_id; delTempCmd.ExecuteNonQuery(); } catch (Exception ex) { } finally { cn.Close(); } //create wall //create wall section , 1 for content, 1 for photosection comingCreateWall.wall_id = wall_id; return(comingCreateWall); }