Changeset 30536 for extensions/Picasa2Piwigo/Picasa2Piwigo/StatusForm.cs
- Timestamp:
- Nov 19, 2014, 4:33:20 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/Picasa2Piwigo/Picasa2Piwigo/StatusForm.cs
r26935 r30536 77 77 { 78 78 case constants.COMMAND_START_UP: 79 WriteLog("Start up");79 logit.WriteLog("Start up"); 80 80 81 81 //1. Reading Picasa contacts … … 91 91 } 92 92 93 logit.WriteLog("Read picasa contacts"); 93 94 //2. Validate that server can be reached 94 95 SetStatus(WinFormStrings.CheckServerConnStrRes); … … 104 105 105 106 //3. Login 107 logit.WriteLog("logging in"); 106 108 SetStatus(WinFormStrings.LogInStrRes); 107 109 step_passed = UpdateStatus(SyncPiwigoLogIn()); … … 117 119 //4. Get Piwigo Version 118 120 PiwigoVersionNumber = ""; 121 logit.WriteLog("getting version number from piwigo"); 119 122 SetStatus(WinFormStrings.GetPiwigoVersionStrRes); 120 123 step_passed = UpdateStatus(SyncPiwigoGetVersion(ref PiwigoVersionNumber)); … … 126 129 127 130 //5. Get list of albums 131 logit.WriteLog("getting list of albums from piwigo"); 128 132 SetStatus(WinFormStrings.RetrieveAlbumListStRes); 129 133 step_passed = UpdateStatus(SyncPiwigoGetAlbums(ref AlbumList)); … … 140 144 141 145 case constants.COMMAND_ADD_TO_ALBUM: 142 WriteLog("Add to Album");146 logit.WriteLog("Add to Album"); 143 147 144 148 //1. Validate that server can be reached … … 182 186 183 187 case constants.COMMAND_NEW_ALBUM: 184 WriteLog("New Album");188 logit.WriteLog("New Album"); 185 189 186 190 //1. Validate that server can be reached … … 482 486 0x70, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x6D, 0x77, 0x67, 0x2D, 0x72, 0x73, 0x3A, 483 487 0x4E, 0x61, 0x6D, 0x65, 0x3D, 0x22 }; 484 int temp1, temp2, k, i, m;488 int temp1, temp2, k, l, i, m, LengthToConvert; 485 489 UInt16 signature, exif_length; 486 490 string name_from_photo, tempstr; 487 byte[] temp_array = new byte[ 1];491 byte[] temp_array = new byte[4]; 488 492 489 493 try … … 517 521 { 518 522 name_from_photo = ""; 519 do 523 //found a name 524 if (k >= exif_length - 5) 520 525 { 521 //found a name 522 temp_array[0] = filedata[k]; 523 tempstr = Encoding.Default.GetString(temp_array); 524 if (tempstr != "\"") 526 //just in case we're trying to read beyond end of filedata 527 //probably will never happen 528 logit.WriteLog("Read to end of xml"); 529 break; 530 } 531 temp_array[0] = filedata[k]; 532 temp_array[1] = filedata[k + 1]; 533 temp_array[2] = filedata[k + 2]; 534 temp_array[3] = filedata[k + 3]; 535 //tempstr = Encoding.UTF8.GetString(filedata); 536 if (exif_length - k > 1024) 537 { 538 LengthToConvert = 1024; 539 } 540 else 541 { 542 LengthToConvert = exif_length - k; 543 } 544 if (LengthToConvert < 3) 545 { 546 logit.WriteLog("Too close to end of xml"); 547 break; 548 } 549 tempstr = Encoding.UTF8.GetString(filedata, k, LengthToConvert); 550 l = tempstr.IndexOf("\""); 551 if (l != -1) 552 { 553 tempstr = tempstr.Substring(0, l); 554 k += tempstr.Length; 555 name_from_photo = tempstr; 556 557 if (name_from_photo.Substring(0, 1) == "0") 525 558 { 526 name_from_photo += tempstr;559 name_from_photo = name_from_photo.Substring(1); 527 560 } 528 else 561 //have name, search for it in array 562 for (i = 0; i < person_count; i++) 529 563 { 530 if (name_from_photo .Substring(0, 1) == "0")564 if (name_from_photo == contact_array[i].name) 531 565 { 532 name_from_photo = name_from_photo.Substring(1); 566 found_contact_in_photo_array[i] = true; 567 break; 533 568 } 534 //have name, search for it in array535 for (i = 0; i < person_count; i++)536 {537 if (name_from_photo == contact_array[i].name)538 {539 found_contact_in_photo_array[i] = true;540 break;541 }542 }543 break;544 569 } 545 k++; 546 } while (tempstr != "\""); 570 } 571 else 572 { 573 //couldn't find end of name, abort 574 logit.WriteLog("couldn't find end of name: " + tempstr); 575 break; 576 } 547 577 } 548 578 k++; … … 603 633 string filepath, inidata, photofilename, photo_file_name, ini_unique_id; 604 634 string face_tag_list; 605 bool[] found_contact_in_photo_array = new bool[ 256];635 bool[] found_contact_in_photo_array = new bool[Picasa2PiwigoConstants.constants.MAX_NUMBER_OF_CONTACTS]; 606 636 bool found_photo; 607 637 bool finished_faces, found_person; … … 616 646 617 647 //if it's a video file then skip it 618 if (photofilename.ToLower().IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) == -1 && 619 photofilename.ToLower().IndexOf(".gif", StringComparison.OrdinalIgnoreCase) == -1 && 620 photofilename.ToLower().IndexOf(".png", StringComparison.OrdinalIgnoreCase) == -1 && 621 photofilename.ToLower().IndexOf(".tga", StringComparison.OrdinalIgnoreCase) == -1 && 622 photofilename.ToLower().IndexOf(".tif", StringComparison.OrdinalIgnoreCase) == -1 && 623 photofilename.ToLower().IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) == -1 && 624 photofilename.ToLower().IndexOf(".psd", StringComparison.OrdinalIgnoreCase) == -1) 648 if (!CheckFileNameExtension(photofilename.ToLower())) 625 649 { 626 650 return face_tag_list; … … 636 660 { 637 661 if (found_contact_in_photo_array[k]) 638 { 662 { 639 663 face_tag_list += RemoveCommaFromContract(GlobalClass.contact_array[k].name) + ","; 640 664 } … … 644 668 if (File.Exists(filepath + "\\.picasa.ini")) 645 669 { 670 logit.WriteLog("picasa.ini file exists"); 646 671 try 647 672 { … … 659 684 else if (File.Exists(filepath + "\\Picasa.ini")) 660 685 { 686 logit.WriteLog("Picasa.ini file exists"); 661 687 try 662 688 { … … 678 704 { 679 705 while ((inidata = inifile.ReadLine()) != null) 680 { 706 { 681 707 if (inidata.Length > 4) 682 708 { … … 684 710 { 685 711 found_photo = false; 686 k = inidata.IndexOf("]"); 712 k = inidata.IndexOf("]"); 687 713 if (k != -1) 688 714 { … … 694 720 } 695 721 696 if (photo_file_name.ToLower().IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) != -1 || 697 photo_file_name.ToLower().IndexOf(".gif", StringComparison.OrdinalIgnoreCase) != -1 || 698 photo_file_name.ToLower().IndexOf(".png", StringComparison.OrdinalIgnoreCase) != -1 || 699 photo_file_name.ToLower().IndexOf(".tga", StringComparison.OrdinalIgnoreCase) != -1 || 700 photo_file_name.ToLower().IndexOf(".tif", StringComparison.OrdinalIgnoreCase) != -1 || 701 photo_file_name.ToLower().IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) != -1 || 702 photo_file_name.ToLower().IndexOf(".psd", StringComparison.OrdinalIgnoreCase) != -1) 722 if (CheckFileNameExtension(photo_file_name.ToLower())) 703 723 { 704 724 found_photo = true; … … 746 766 { 747 767 face_tag_list += GlobalClass.contact_array[l].name + ","; 768 logit.WriteLog("face_tag_list: " + face_tag_list); 748 769 } 749 770 } … … 763 784 if (face_tag_list.Length > 1) 764 785 { 786 logit.WriteLog("end face_tag_list: " + face_tag_list); 765 787 face_tag_list = face_tag_list.Substring(0, face_tag_list.Length - 1); 766 788 } … … 820 842 GlobalClass.picasa_unique_id_arrary[GlobalClass.unique_id_count].index = GlobalClass.person_count; 821 843 GlobalClass.person_count++; 844 if (GlobalClass.person_count >= constants.MAX_NUMBER_OF_CONTACTS) 845 { 846 GlobalClass.StatusDialogErrorMessage = WinFormStrings.TooManyPeopleTags; 847 return false; 848 } 822 849 } 823 850 else … … 837 864 838 865 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.getStatus"; 839 WriteLog(WebRequest);866 logit.WriteLog(WebRequest); 840 867 841 868 WebResponse = SendWebGet(WebRequest); 842 WriteLog(WebResponse);869 logit.WriteLog(WebResponse); 843 870 844 871 if (WebResponse.IndexOf("An error occurred:") != -1) … … 865 892 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.login"; 866 893 PostData = "username=" + FormSettings.Default.PiwigoUserName + "&password=" + FormSettings.Default.PiwigoPassword; 867 WriteLog(WebRequest);868 WriteLog(PostData);894 logit.WriteLog(WebRequest); 895 logit.WriteLog("username=" + FormSettings.Default.PiwigoUserName + "&password=" + "*****"); 869 896 response = SendWebPost(WebRequest, PostData, "application/x-www-form-urlencoded"); 870 897 if (!response) 871 898 { 872 WriteLog("Log In Post Fail.");899 logit.WriteLog("Log In Post Fail."); 873 900 return false; 874 901 } 875 902 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.session.getStatus"; 876 WriteLog(WebRequest);903 logit.WriteLog(WebRequest); 877 904 WebResponse = SendWebGet(WebRequest); 878 WriteLog(WebResponse);905 logit.WriteLog(WebResponse); 879 906 if (WebResponse.IndexOf("An error occurred:") != -1) 880 907 { … … 896 923 897 924 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.getVersion"; 898 WriteLog(WebRequest);925 logit.WriteLog(WebRequest); 899 926 900 927 WebResponse = SendWebGet(WebRequest); 901 WriteLog(WebResponse);928 logit.WriteLog(WebResponse); 902 929 903 930 PiwigoVersionNumber = ""; … … 935 962 } 936 963 937 WriteLog(WebRequest);964 logit.WriteLog(WebRequest); 938 965 939 966 WebResponse = SendWebGet(WebRequest); 940 WriteLog(WebResponse);967 logit.WriteLog(WebResponse); 941 968 942 969 if (WebResponse.IndexOf("An error occurred:") != -1) … … 959 986 960 987 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.tags.getList"; 961 WriteLog(WebRequest);988 logit.WriteLog(WebRequest); 962 989 963 990 WebResponse = SendWebGet(WebRequest); 964 WriteLog(WebResponse);991 logit.WriteLog(WebResponse); 965 992 966 993 if (WebResponse.IndexOf("An error occurred:") != -1) … … 1004 1031 } 1005 1032 1006 WriteLog(WebRequest);1033 logit.WriteLog(WebRequest); 1007 1034 1008 1035 WebResponse = SendWebGet(WebRequest); 1009 WriteLog(WebResponse);1036 logit.WriteLog(WebResponse); 1010 1037 1011 1038 if (WebResponse.IndexOf("An error occurred:") != -1) … … 1030 1057 string [] file_to_send = {local_command_line_args[0]}; 1031 1058 string[,] post_array = new string[10,2]; 1032 string image_id ;1059 string image_id, filtered_xml; 1033 1060 bool upload_failed, response; 1034 1061 string face_tag_list, tag_list, comment_list, all_tags; … … 1044 1071 SetStatus(WinFormStrings.UploadingPHotoStrRes + " " + (i + 1).ToString() + " " + WinFormStrings.ofStrREf + " " + local_command_line_args.Length.ToString() + "..."); 1045 1072 1046 face_tag_list = SyncPiwigoGetFaceTags(local_command_line_args[i]);1047 1048 tag_list = "";1049 comment_list = "";1050 all_tags = "";1051 SyncPiwigoGetTagsandComments(ref tag_list, ref comment_list, local_command_line_args[i]);1052 1053 if (face_tag_list.Length != 0)1054 {1055 all_tags = face_tag_list;1056 }1057 if (tag_list.Length != 0)1058 {1059 if (all_tags.Length != 0)1060 {1061 all_tags += ",";1062 }1063 all_tags += tag_list;1064 }1065 1066 1073 k = local_command_line_args[i].LastIndexOf("\\"); 1067 1074 filename = local_command_line_args[i].Substring(k + 1); 1068 PhotoFileContents = FileToByteArray(local_command_line_args[i]); 1069 1070 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.images.addSimple"; 1071 1072 WebResponse = ""; 1073 local_command_line_args[i] = local_command_line_args[i].Replace("\\","\\\\"); 1074 post_array[0, 0] = "name=\"category\""; 1075 post_array[0, 1] = NewPiwigoAlbumId.ToString(); 1076 post_array[1, 0] = "name=\"name\"" ; 1077 post_array[1, 1] = filename; 1078 k = 2; 1079 if (all_tags.Length != 0) 1080 { 1081 post_array[k, 0] = "name=\"tags\""; 1082 post_array[k, 1] = all_tags; 1083 k++; 1084 } 1085 if (comment_list.Length != 0) 1086 { 1087 post_array[k, 0] = "name=\"comment\""; 1088 post_array[k, 1] = "\"" + comment_list + "\""; 1089 k++; 1090 } 1091 post_array[k, 0] = "name=\"image\"" + ";filename=" + "\"" + local_command_line_args[i] + "\""; 1092 1093 WriteLog(WebRequest); 1094 for (di = 0; di < k; di++) 1095 { 1096 WriteLog(post_array[k, 0] + post_array[k, 1]); 1097 } 1098 1099 1100 response = SendWebPostMultipart(WebRequest, post_array, "multipart/form-data", PhotoFileContents, 1101 ref WebResponse); 1102 1103 WriteLog(WebResponse); 1104 1105 if (!response) 1106 { 1107 return false; 1108 } 1109 xmlResponse = XmlGetAttribute(FilterForXML(WebResponse), "stat"); 1110 if (xmlResponse == "ok") 1111 { 1112 image_id = XmlGetElement(FilterForXML(WebResponse), "image_id"); 1113 } 1114 else 1115 { 1116 upload_failed = true; 1117 } 1075 if (CheckFileNameExtension(filename.ToLower())) 1076 { 1077 face_tag_list = SyncPiwigoGetFaceTags(local_command_line_args[i]); 1078 1079 tag_list = ""; 1080 comment_list = ""; 1081 all_tags = ""; 1082 SyncPiwigoGetTagsandComments(ref tag_list, ref comment_list, local_command_line_args[i]); 1083 1084 if (face_tag_list.Length != 0) 1085 { 1086 all_tags = face_tag_list; 1087 } 1088 if (tag_list.Length != 0) 1089 { 1090 if (all_tags.Length != 0) 1091 { 1092 all_tags += ","; 1093 } 1094 all_tags += tag_list; 1095 } 1096 1097 k = local_command_line_args[i].LastIndexOf("\\"); 1098 filename = local_command_line_args[i].Substring(k + 1); 1099 PhotoFileContents = FileToByteArray(local_command_line_args[i]); 1100 1101 WebRequest = FormSettings.Default.PiwigoUrlSetting + "/ws.php?method=pwg.images.addSimple"; 1102 1103 WebResponse = ""; 1104 local_command_line_args[i] = local_command_line_args[i].Replace("\\", "\\\\"); 1105 post_array[0, 0] = "name=\"category\""; 1106 post_array[0, 1] = NewPiwigoAlbumId.ToString(); 1107 post_array[1, 0] = "name=\"name\""; 1108 post_array[1, 1] = filename; 1109 k = 2; 1110 if (all_tags.Length != 0) 1111 { 1112 post_array[k, 0] = "name=\"tags\""; 1113 post_array[k, 1] = all_tags; 1114 k++; 1115 } 1116 if (comment_list.Length != 0) 1117 { 1118 post_array[k, 0] = "name=\"comment\""; 1119 post_array[k, 1] = "\"" + comment_list + "\""; 1120 k++; 1121 } 1122 post_array[k, 0] = "name=\"image\"" + ";filename=" + "\"" + local_command_line_args[i] + "\""; 1123 1124 logit.WriteLog(WebRequest); 1125 for (di = 0; di < k; di++) 1126 { 1127 logit.WriteLog(post_array[di, 0] + " " + post_array[di, 1]); 1128 } 1129 1130 1131 response = SendWebPostMultipart(WebRequest, post_array, "multipart/form-data", PhotoFileContents, 1132 ref WebResponse); 1133 1134 logit.WriteLog(WebResponse); 1135 1136 GlobalClass.StatusDialogErrorMessage = WinFormStrings.UploadUnableToUploadStrRes; 1137 1138 if (!response) 1139 { 1140 return false; 1141 } 1142 1143 filtered_xml = FilterForXML(WebResponse); 1144 if (filtered_xml.Length == 0) 1145 { 1146 logit.WriteLog("No xml response after upload."); 1147 return false; 1148 } 1149 1150 xmlResponse = XmlGetAttribute(filtered_xml, "stat"); 1151 if (xmlResponse == "ok") 1152 { 1153 image_id = XmlGetElement(FilterForXML(WebResponse), "image_id"); 1154 } 1155 else 1156 { 1157 upload_failed = true; 1158 } 1159 } 1118 1160 } 1119 1161 return !upload_failed; … … 1126 1168 try 1127 1169 { 1170 ServicePointManager.Expect100Continue = false; 1128 1171 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 1129 1172 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; … … 1133 1176 request.Timeout = 60000; 1134 1177 request.Credentials = CredentialCache.DefaultCredentials; 1178 request.Referer = "user-agent"; 1179 request.UserAgent = "Mozilla/5.0"; 1135 1180 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 1136 1181 … … 1154 1199 { 1155 1200 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webrequest); 1201 ServicePointManager.Expect100Continue = false; 1156 1202 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 1157 1203 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; … … 1161 1207 request.Method = "POST"; 1162 1208 request.Credentials = CredentialCache.DefaultCredentials; 1209 request.Referer = "user-agent"; 1210 request.UserAgent = "Mozilla/5.0"; 1163 1211 1164 1212 // Create POST data and convert it to a byte array. … … 1217 1265 { 1218 1266 XmlDocument StatusResponse = new XmlDocument(); 1219 StatusResponse.LoadXml(xmlstring); 1220 XmlNodeList nodelist = StatusResponse.GetElementsByTagName(element); 1221 foreach (XmlNode anode in nodelist) 1222 { 1223 return anode.ChildNodes[0].Value; 1267 1268 if (xmlstring.Length != 0) 1269 { 1270 StatusResponse.LoadXml(xmlstring); 1271 XmlNodeList nodelist = StatusResponse.GetElementsByTagName(element); 1272 foreach (XmlNode anode in nodelist) 1273 { 1274 return anode.ChildNodes[0].Value; 1275 } 1224 1276 } 1225 1277 return ""; … … 1284 1336 { 1285 1337 int i; 1338 ServicePointManager.Expect100Continue = false; 1286 1339 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 1287 1340 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; … … 1298 1351 request.Credentials = CredentialCache.DefaultCredentials; 1299 1352 request.KeepAlive = true; 1353 request.Referer = "user-agent"; 1354 request.UserAgent = "Mozilla/5.0"; 1300 1355 1301 1356 try … … 1420 1475 return true; 1421 1476 } 1477 public bool CheckFileNameExtension(string photofilename) 1478 { 1479 if (photofilename.IndexOf(".jpg", StringComparison.OrdinalIgnoreCase) == -1 && 1480 photofilename.IndexOf(".jpeg", StringComparison.OrdinalIgnoreCase) == -1 && 1481 photofilename.IndexOf(".gif", StringComparison.OrdinalIgnoreCase) == -1 && 1482 photofilename.IndexOf(".png", StringComparison.OrdinalIgnoreCase) == -1 && 1483 photofilename.IndexOf(".tga", StringComparison.OrdinalIgnoreCase) == -1 && 1484 photofilename.IndexOf(".tif", StringComparison.OrdinalIgnoreCase) == -1 && 1485 photofilename.IndexOf(".tiff", StringComparison.OrdinalIgnoreCase) == -1 && 1486 photofilename.IndexOf(".psd", StringComparison.OrdinalIgnoreCase) == -1) 1487 { 1488 return false; 1489 } 1490 return true; 1491 } 1422 1492 public string FilterForXML(string input_xml) 1423 1493 { … … 1433 1503 else 1434 1504 { 1435 WriteLog("No xml found in string");1505 logit.WriteLog("No xml found in string"); 1436 1506 } 1437 1507 return xml_only_str; 1438 }1439 public void WriteLog(string logMessage)1440 {1441 string strLogFile;1442 1443 strLogFile = "c:\\temp\\picasa2piwigo_log.txt";1444 #pragma warning disable 01621445 if (constants.DEBUG_LOG_ENABLED)1446 {1447 try1448 {1449 #pragma warning restore 01621450 StreamWriter swLog;1451 1452 logMessage = string.Format("{0}: {1}", DateTime.Now, logMessage);1453 1454 if (!File.Exists(strLogFile))1455 {1456 swLog = new StreamWriter(strLogFile);1457 }1458 else1459 {1460 swLog = File.AppendText(strLogFile);1461 }1462 1463 swLog.WriteLine(logMessage);1464 swLog.WriteLine();1465 1466 swLog.Close();1467 1468 }1469 catch1470 {1471 }1472 }1473 1508 } 1474 1509 }
Note: See TracChangeset
for help on using the changeset viewer.