From 8258269d051ba19a5166eb5523489be2264c1427 Mon Sep 17 00:00:00 2001 From: Jamie Nadeau Date: Wed, 28 Sep 2016 13:37:40 -0400 Subject: [PATCH] Performance increase and other goodies - Performance increase on processing duplicates - Automated OSM login (fixes #2 kind of) - Info on what is processing in duplicates --- .../ca/osmcanada/osvuploadr/API/OSMApi.java | 2 +- .../osmcanada/osvuploadr/FolderCleaner.java | 12 + .../java/ca/osmcanada/osvuploadr/JFMain.java | 21 +- .../ca/osmcanada/osvuploadr/JPInfoBox.form | 55 ++ .../ca/osmcanada/osvuploadr/JPInfoBox.java | 73 ++ .../java/ca/osmcanada/osvuploadr/JPMain.form | 42 +- .../java/ca/osmcanada/osvuploadr/JPMain.java | 364 ++++++-- .../ca/osmcanada/osvuploadr/JPMain.java.orig | 781 ------------------ 8 files changed, 471 insertions(+), 879 deletions(-) create mode 100644 OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form create mode 100644 OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java delete mode 100644 OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java.orig diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java index 3320e9e..d5a992e 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/API/OSMApi.java @@ -11,7 +11,7 @@ * @author Jamie Nadeau */ public class OSMApi extends DefaultApi10a { - private static final String AUTHORIZE_URL = "http://www.openstreetmap.org/oauth/authorize?oauth_token=%s"; + private static final String AUTHORIZE_URL = "https://www.openstreetmap.org/oauth/authorize?oauth_token=%s"; private static final String REQUEST_TOKEN_RESOURCE = "www.openstreetmap.org/oauth/request_token"; private static final String ACCESS_TOKEN_RESOURCE = "www.openstreetmap.org/oauth/access_token"; diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java index 05721b4..675e859 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/FolderCleaner.java @@ -32,6 +32,7 @@ public class FolderCleaner { int dist_threshold = 4; // Minimum distance a photo should move to not be considered a duplicate (in meters) int radius_threshold = 20; // Minimum turn radius a photo should move to not be considered a duplicate (degrees) String duplicate_folder="duplicates"; + JFMain info=null; private double calc_distance(double lon1, double lat1, double lon2, double lat2){ //haversine formula @@ -85,6 +86,9 @@ public int compare(File f1, File f2) }}); for(File f:file_list){ + if(info!=null){ + info.SetInfoBoxText(f.getPath()); + } ImageProperties imp = Helper.getImageProperties(f); if(is_first){ is_first=false; @@ -151,10 +155,18 @@ public void RemoveDuplicates(){ } } do_science(); + if(info!=null){ + info.SetInfoBoxText("Done"); + } + } + + public void setInfoBox(JFMain frame){ + info=frame; } public FolderCleaner(String Folder){ _folder=Folder; } + } diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java index 799f3ee..c1fb2db 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JFMain.java @@ -12,7 +12,7 @@ * @author Jamie Nadeau */ public class JFMain extends javax.swing.JFrame { - + static JPInfoBox jib = new JPInfoBox(); /** * Creates new form JFMain */ @@ -22,7 +22,20 @@ public JFMain() { this.setSize(640,320); this.setTitle("OSVUploadr"); } - + + public void ShowInfoBox(){ + jib.setVisible(true); + this.setSize(640,470); + this.pack(); + } + public void HideInfoBox(){ + jib.setVisible(false); + this.setSize(640,320); + } + + public void SetInfoBoxText(String str){ + jib.SetProcessingText(str); + } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -81,7 +94,9 @@ public void run() { JFMain jf =new JFMain(); JPMain jp = new JPMain(); jp.setVisible(true); - jf.add(jp,BorderLayout.CENTER); + jib.setVisible(false); + jf.add(jp,BorderLayout.NORTH); + jf.add(jib,BorderLayout.SOUTH); jf.setVisible(true); } }); diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form new file mode 100644 index 0000000..348d4e1 --- /dev/null +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.form @@ -0,0 +1,55 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java new file mode 100644 index 0000000..0f1ac45 --- /dev/null +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPInfoBox.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package ca.osmcanada.osvuploadr; + +import javax.swing.UIManager; + +/** + * + * @author Nadeaj + */ +public class JPInfoBox extends javax.swing.JPanel { + + /** + * Creates new form JPInfoBox + */ + public JPInfoBox() { + try{ + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch(Exception ex){} + initComponents(); + } + + public void SetProcessingText(String str){ + jlProcessing.setText(str); + } + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jlProcessing = new javax.swing.JLabel(); + + setPreferredSize(new java.awt.Dimension(623, 150)); + + jLabel1.setText("Currently Processing:"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jlProcessing, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jlProcessing, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap(78, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jlProcessing; + // End of variables declaration//GEN-END:variables +} diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form index f8418b0..daddf51 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.form @@ -26,16 +26,16 @@ - - + + - - - + + + @@ -53,16 +53,16 @@ - - + + - + - + - + - + @@ -71,7 +71,7 @@ - + @@ -85,7 +85,7 @@ - + @@ -101,15 +101,15 @@ - + - + - + @@ -120,10 +120,10 @@ - + - + @@ -134,15 +134,15 @@ - + - + - + diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java index e63e0e4..382d7ed 100644 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java +++ b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java @@ -30,7 +30,6 @@ import java.util.Comparator; import java.util.List; import ca.osmcanada.osvuploadr.struct.ImageProperties; -import com.drew.metadata.exif.ExifSubIFDDirectory; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; @@ -49,21 +48,25 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import ca.osmcanada.osvuploadr.Utils.*; import ca.osmcanada.osvuploadr.struct.PageContent; +import java.nio.charset.Charset; import org.apache.http.client.HttpClient; -import org.apache.http.HttpException; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.CookieStore; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.HttpClientBuilder; + /** * * @author Jamie Nadeau */ public class JPMain extends javax.swing.JPanel { - private final String BASE_URL="http://www.openstreetmap.org/"; + private final String BASE_URL="https://www.openstreetmap.org/"; private final String URL_SEQUENCE = "http://openstreetview.com/1.0/sequence/"; private final String URL_PHOTO = "http://openstreetview.com/1.0/photo/"; private final String URL_FINISH = "http://openstreetview.com/1.0/sequence/finished-uploading/"; @@ -94,11 +97,95 @@ public String GetOSMUser(String usr, String psw)throws IOException{ //Automated grant and login*************************************** //Get logon form - HttpClient client = HttpClientBuilder.create().build(); + CookieStore httpCookieStore = new BasicCookieStore(); + HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build(); //post.setHeader("User-Agent", USER_AGENT); try{ PageContent pc = Helper.GetPageContent(url,client); - System.out.println(pc.GetPage()); + + List cookies = httpCookieStore.getCookies(); + System.out.println("Getting OSM Login page"); + String page = pc.GetPage(); + + System.out.println("Sending username and password"); + + int start_indx = page.indexOf("value=\"",page.indexOf("name=\"authenticity_token\""))+7; + int end_indx = page.indexOf("\"",start_indx); + + String utf8 = "✓"; + String authenticity_token = page.substring(start_indx,end_indx); + + start_indx = page.indexOf("value=\"",page.indexOf("name=\"referer\""))+7; + end_indx = page.indexOf("\"",start_indx); + String referer = page.substring(start_indx,end_indx); + + Map arguments = new HashMap<>(); + arguments.put("utf8", utf8); + arguments.put("authenticity_token", authenticity_token); + arguments.put("referer", referer); + arguments.put("username", usr); + arguments.put("password", psw); + arguments.put("commit", "Login"); + arguments.put("openid_url", ""); + + System.out.println("Logging in"); + page = sendForm(BASE_URL+"login",arguments,"POST",cookies); + + + + //Proccessing grant access page + System.out.println("Processing Granting Access page"); + start_indx = page.indexOf("",start_indx); + String tmp = page.substring(start_indx,end_indx); //Get aproximate location of pin code + + Pattern p = Pattern.compile("([A-Za-z0-9]){15,25}"); + Matcher m = p.matcher(tmp); + String pin = ""; + if(m.find()) + { + pin = m.group(); + } + final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin); + return accessToken.getToken()+"|"+accessToken.getTokenSecret(); + } catch(Exception ex) { @@ -123,21 +210,111 @@ public String GetOSMUser() throws IOException{ //java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); String pin_code = JOptionPane.showInputDialog(null, "Authorization window has opened, please paste authorization code below once authorized.\nWith out the \".\" at the end", "Authorization Required", JOptionPane.INFORMATION_MESSAGE); final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin_code); - final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service); - service.signRequest(accessToken, request); - Response response = request.send(); - String body = response.getBody(); - int indxUserId= body.indexOf("user id=\""); - int indxEndUserId = body.indexOf("\"",indxUserId+9); - int indxUsername= body.indexOf("display_name=\""); - int indxEndUsername = body.indexOf("\"",indxUsername+14); + //final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service); + //service.signRequest(accessToken, request); + //Response response = request.send(); + //String body = response.getBody(); + //int indxUserId= body.indexOf("user id=\""); + //int indxEndUserId = body.indexOf("\"",indxUserId+9); + //int indxUsername= body.indexOf("display_name=\""); + //int indxEndUsername = body.indexOf("\"",indxUsername+14); //String user_id = body.substring(indxUserId+9,indxEndUserId); //String username = body.substring(indxUsername+14,indxEndUsername); //return user_id +";"+username; return accessToken.getToken()+"|"+accessToken.getTokenSecret(); - } + } + + private int FindOpening(String str,int current_index,String OpeningChar){ + for(int i=current_index;i>=0;i--){ + if(str.substring(i,i+1).equals("<")){ + return i; + } + } + return 0; + } + private String sendForm(String target_url,Map arguments,String method,List cookielist){ + try{ + URL url = new URL(target_url); + HttpURLConnection con = (HttpURLConnection)url.openConnection(); + //HttpURLConnection http = (HttpURLConnection)con; + con.setRequestMethod(method); // PUT is another valid option + con.setDoOutput(true); + con.setInstanceFollowRedirects(false); + String cookiestr=""; + if(cookielist!=null){ + if(cookielist.size()>0){ + for(Cookie cookie:cookielist){ + if(!cookiestr.equals("")){ + cookiestr+=";" + cookie.getName() +"=" +cookie.getValue(); + } + else{ + cookiestr+=cookie.getName() +"=" +cookie.getValue(); + } + } + con.setRequestProperty("Cookie", cookiestr); + } + } + + con.setReadTimeout(5000); + + StringJoiner sj = new StringJoiner("&"); + for(Map.Entry entry : arguments.entrySet()) + sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); + byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); + int length = out.length; + + con.setFixedLengthStreamingMode(length); + con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + con.setRequestProperty("Accept-Language", "en-us;"); + con.connect(); + try(OutputStream os = con.getOutputStream()) { + os.write(out); + os.close(); + } + + boolean redirect = false; + int status = con.getResponseCode(); + + if(status != HttpURLConnection.HTTP_OK) { + if (status == HttpURLConnection.HTTP_MOVED_TEMP + || status == HttpURLConnection.HTTP_MOVED_PERM + || status == HttpURLConnection.HTTP_SEE_OTHER) + redirect = true; + } + + if(redirect){ + String newURL = con.getHeaderField("Location"); + String cookies = con.getHeaderField("Set-Cookie"); + if(cookies == null){ + cookies= cookiestr; + } + con = (HttpURLConnection) new URL(newURL).openConnection(); + con.setRequestProperty("Cookie", cookies); + } + + InputStream is = con.getInputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte buf[] = new byte[1024]; + int letti; + + while ((letti = is.read(buf)) > 0) + baos.write(buf, 0, letti); + + String data = new String(baos.toByteArray(),Charset.forName("UTF-8")); + con.disconnect(); + + return data; + + } + catch(Exception ex){ + Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); + } + return ""; + } + private void sendFile(OutputStream out, String name, InputStream in, String fileName) { try { String o = "Content-Disposition: form-data; name=\"" + URLEncoder.encode(name,"UTF-8") + "\"; filename=\"" + URLEncoder.encode(fileName,"UTF-8") + "\"\r\nContent-Type: image/jpeg\r\n\r\n"; @@ -545,21 +722,21 @@ public int compare(File f1, File f2) // //GEN-BEGIN:initComponents private void initComponents() { - jButton1 = new javax.swing.JButton(); + jbAdd = new javax.swing.JButton(); listDir = new java.awt.List(); jLabel1 = new javax.swing.JLabel(); - jButton2 = new javax.swing.JButton(); - jButton3 = new javax.swing.JButton(); - jButton4 = new javax.swing.JButton(); - jButton5 = new javax.swing.JButton(); - - jButton1.setText("Add Folder"); - jButton1.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton1.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton1.setPreferredSize(new java.awt.Dimension(123, 23)); - jButton1.addActionListener(new java.awt.event.ActionListener() { + jbRemoveDup = new javax.swing.JButton(); + jbUpload = new javax.swing.JButton(); + jbExit = new javax.swing.JButton(); + jbRemove = new javax.swing.JButton(); + + jbAdd.setText("Add Folder"); + jbAdd.setMaximumSize(new java.awt.Dimension(123, 23)); + jbAdd.setMinimumSize(new java.awt.Dimension(123, 23)); + jbAdd.setPreferredSize(new java.awt.Dimension(123, 23)); + jbAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); + jbAddActionPerformed(evt); } }); @@ -572,35 +749,35 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText("Directories"); - jButton2.setText("Remove Duplicates"); - jButton2.addActionListener(new java.awt.event.ActionListener() { + jbRemoveDup.setText("Remove Duplicates"); + jbRemoveDup.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton2ActionPerformed(evt); + jbRemoveDupActionPerformed(evt); } }); - jButton3.setText("Upload"); - jButton3.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton3.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton3.addActionListener(new java.awt.event.ActionListener() { + jbUpload.setText("Upload"); + jbUpload.setMaximumSize(new java.awt.Dimension(123, 23)); + jbUpload.setMinimumSize(new java.awt.Dimension(123, 23)); + jbUpload.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton3ActionPerformed(evt); + jbUploadActionPerformed(evt); } }); - jButton4.setText("Exit"); - jButton4.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton4.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton4.addActionListener(new java.awt.event.ActionListener() { + jbExit.setText("Exit"); + jbExit.setMaximumSize(new java.awt.Dimension(123, 23)); + jbExit.setMinimumSize(new java.awt.Dimension(123, 23)); + jbExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton4ActionPerformed(evt); + jbExitActionPerformed(evt); } }); - jButton5.setText("Remove Folder"); - jButton5.addActionListener(new java.awt.event.ActionListener() { + jbRemove.setText("Remove Folder"); + jbRemove.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton5ActionPerformed(evt); + jbRemoveActionPerformed(evt); } }); @@ -618,14 +795,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(layout.createSequentialGroup() .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) + .addComponent(jbAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jbRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))) + .addComponent(jbRemoveDup, javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(jbUpload, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbExit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( @@ -637,15 +814,15 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(listDir, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton5) + .addComponent(jbRemove) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton2) + .addComponent(jbRemoveDup) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jbUpload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(jbExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(41, Short.MAX_VALUE)) ); }// //GEN-END:initComponents @@ -654,12 +831,12 @@ private void listDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST // TODO add your handling code here: }//GEN-LAST:event_listDirActionPerformed - private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed + private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExitActionPerformed // TODO add your handling code here: System.exit(0); - }//GEN-LAST:event_jButton4ActionPerformed + }//GEN-LAST:event_jbExitActionPerformed - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + private void jbAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAddActionPerformed JFileChooser fc = new JFileChooser(); if(!last_dir.isEmpty()){ fc.setCurrentDirectory(new java.io.File(last_dir)); // start at application current directory @@ -688,9 +865,9 @@ public boolean accept(File current, String name){ } } } - }//GEN-LAST:event_jButton1ActionPerformed + }//GEN-LAST:event_jbAddActionPerformed - private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed + private void jbRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRemoveActionPerformed int selectedCnt = listDir.getSelectedItems().length; if(selectedCnt > 0) { @@ -699,9 +876,9 @@ private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS listDir.remove(listDir.getSelectedIndexes()[i]); } } - }//GEN-LAST:event_jButton5ActionPerformed + }//GEN-LAST:event_jbRemoveActionPerformed - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed + private void jbUploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbUploadActionPerformed String path = ca.osmcanada.osvuploadr.JFMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = ""; try{ @@ -717,8 +894,40 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS if(!id.exists()) { try{ + String[] buttons={"Automatically","Manually","Cancel"}; + int rc = JOptionPane.showOptionDialog(null,"You must login to OSM to continue the upload. Would you like the automated version or the manual version?","Confirmation",JOptionPane.INFORMATION_MESSAGE,0,null,buttons,buttons[0]); + String token=""; System.out.println("GetOSMUser"); - String token = GetOSMUser(); + switch(rc){ + case 0: + String usr = ""; + String psw = ""; + JTextField tf = new JTextField(); + JPasswordField pf = new JPasswordField(); + rc = JOptionPane.showConfirmDialog(null,tf,"Enter email address or OSM username", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); + if(rc == JOptionPane.OK_OPTION){ + usr = tf.getText(); + } + else{ + return; + } + + rc = JOptionPane.showConfirmDialog(null,pf,"Please enter password", JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE); + if(rc == JOptionPane.OK_OPTION){ + psw = new String(pf.getPassword()); + } + else{ + return; + } + token = GetOSMUser(usr,psw); + break; + case 1: + token = GetOSMUser(); + break; + case 2: + return; + + } Path targetPath = Paths.get("./access_token.txt"); byte[] bytes = token.split("\\|")[0].getBytes(StandardCharsets.UTF_8); Files.write(targetPath, bytes, StandardOpenOption.CREATE); @@ -754,23 +963,32 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS //um = new UploadManager(listDir.getItems()); //um.start(); - }//GEN-LAST:event_jButton3ActionPerformed + }//GEN-LAST:event_jbUploadActionPerformed - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + private void jbRemoveDupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRemoveDupActionPerformed + JFMain topframe = (JFMain)SwingUtilities.getWindowAncestor(this); + topframe.ShowInfoBox(); + topframe.SetInfoBoxText("test"); for(String item:listDir.getItems()){ - FolderCleaner fc = new FolderCleaner(item); - fc.RemoveDuplicates(); - } - }//GEN-LAST:event_jButton2ActionPerformed + Thread t = new Thread(){ + public void run(){ + FolderCleaner fc = new FolderCleaner(item); + fc.setInfoBox(topframe); + fc.RemoveDuplicates(); + } + }; + t.start(); + } + }//GEN-LAST:event_jbRemoveDupActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton jButton1; - private javax.swing.JButton jButton2; - private javax.swing.JButton jButton3; - private javax.swing.JButton jButton4; - private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; + private javax.swing.JButton jbAdd; + private javax.swing.JButton jbExit; + private javax.swing.JButton jbRemove; + private javax.swing.JButton jbRemoveDup; + private javax.swing.JButton jbUpload; private java.awt.List listDir; // End of variables declaration//GEN-END:variables } diff --git a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java.orig b/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java.orig deleted file mode 100644 index 5b1bfe1..0000000 --- a/OSVUploadr/src/main/java/ca/osmcanada/osvuploadr/JPMain.java.orig +++ /dev/null @@ -1,781 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package ca.osmcanada.osvuploadr; -import javax.swing.*; -import java.io.File; -import java.lang.*; -import ca.osmcanada.osvuploadr.API.OSMApi; -import ca.osmcanada.osvuploadr.UploadManager; -import com.drew.imaging.ImageMetadataReader; -import com.drew.metadata.Metadata; -import com.drew.metadata.exif.GpsDirectory; -import com.github.scribejava.core.builder.ServiceBuilder; -import com.github.scribejava.core.model.OAuth1AccessToken; -import com.github.scribejava.core.model.OAuth1RequestToken; -import com.github.scribejava.core.model.OAuthRequest; -import com.github.scribejava.core.model.Response; -import com.github.scribejava.core.model.Verb; -import com.github.scribejava.core.oauth.OAuth10aService; -import java.io.FilenameFilter; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import ca.osmcanada.osvuploadr.struct.ImageProperties; -import com.drew.metadata.exif.ExifSubIFDDirectory; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.math.RoundingMode; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.text.DecimalFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.StringJoiner; -import java.util.UUID; -import java.util.logging.Level; -import java.util.logging.Logger; -import ca.osmcanada.osvuploadr.Utils.*; -import ca.osmcanada.osvuploadr.struct.PageContent; -import org.apache.http.client.HttpClient; -import org.apache.http.HttpException; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.HttpClientBuilder; - -/** - * - * @author Jamie Nadeau - */ -public class JPMain extends javax.swing.JPanel { - - private final String BASE_URL="http://www.openstreetmap.org/"; - private final String URL_SEQUENCE = "http://openstreetview.com/1.0/sequence/"; - private final String URL_PHOTO = "http://openstreetview.com/1.0/photo/"; - private final String URL_FINISH = "http://openstreetview.com/1.0/sequence/finished-uploading/"; -<<<<<<< HEAD - private final String URL_ACCESS = "http://openstreetview.com/auth/openstreetmap/client_auth"; - private final String API_KEY = "rBWV8Eaottv44tXfdLofdNvVemHOL62Lsutpb9tw"; - private final String API_SECRET = "rpmeZIp49sEjjcz91X9dsY0vD1PpEduixuPy8T6S"; -======= - private final String API_KEY = "[API KEY HERE]"; - private final String API_SECRET = "[SECRET KEY HERE]"; ->>>>>>> a729213f18b2ea4e3764f6d54d02fe2315a0ad2a - private String last_dir =""; - - UploadManager um; - /** - * Creates new form JPMain - */ - public JPMain() { - try{ - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } - catch(Exception ex){} - initComponents(); - } - - public String GetOSMUser(String usr, String psw)throws IOException{ - final OAuth10aService service = new ServiceBuilder() - .apiKey(API_KEY) - .apiSecret(API_SECRET) - .build(OSMApi.instance()); - final OAuth1RequestToken requestToken = service.getRequestToken(); - String url = service.getAuthorizationUrl(requestToken); - - //Automated grant and login*************************************** - //Get logon form - HttpClient client = HttpClientBuilder.create().build(); - //post.setHeader("User-Agent", USER_AGENT); - try{ - PageContent pc = Helper.GetPageContent(url,client); - System.out.println(pc.GetPage()); - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - - - - //End Automated grant and login ********************************** - return ""; - } - - public String GetOSMUser() throws IOException{ - final OAuth10aService service = new ServiceBuilder() - .apiKey(API_KEY) - .apiSecret(API_SECRET) - .build(OSMApi.instance()); - - final OAuth1RequestToken requestToken = service.getRequestToken(); - String url = service.getAuthorizationUrl(requestToken); - Helper.OpenBrowser(java.net.URI.create(url)); - //java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); - String pin_code = JOptionPane.showInputDialog(null, "Authorization window has opened, please paste authorization code below once authorized.\nWith out the \".\" at the end", "Authorization Required", JOptionPane.INFORMATION_MESSAGE); - final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, pin_code); - final OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + "api/0.6/user/details", service); - service.signRequest(accessToken, request); - Response response = request.send(); - String body = response.getBody(); - int indxUserId= body.indexOf("user id=\""); - int indxEndUserId = body.indexOf("\"",indxUserId+9); - int indxUsername= body.indexOf("display_name=\""); - int indxEndUsername = body.indexOf("\"",indxUsername+14); - //String user_id = body.substring(indxUserId+9,indxEndUserId); - //String username = body.substring(indxUsername+14,indxEndUsername); - - //return user_id +";"+username; - return accessToken.getToken()+"|"+accessToken.getTokenSecret(); - } - - private void sendFile(OutputStream out, String name, InputStream in, String fileName) { - try { - String o = "Content-Disposition: form-data; name=\"" + URLEncoder.encode(name,"UTF-8") + "\"; filename=\"" + URLEncoder.encode(fileName,"UTF-8") + "\"\r\nContent-Type: image/jpeg\r\n\r\n"; - out.write(o.getBytes(StandardCharsets.UTF_8)); - byte[] buffer = new byte[2048]; - for (int n = 0; n >= 0; n = in.read(buffer)) - out.write(buffer, 0, n); - out.write("\r\n".getBytes(StandardCharsets.UTF_8)); - } catch (Exception ex) { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - - private void sendField(OutputStream out, String name, String field) { - try { - String o = "Content-Disposition: form-data; name=\"" + URLEncoder.encode(name,"UTF-8") + "\"\r\n\r\n"; - out.write(o.getBytes(StandardCharsets.UTF_8)); - out.write(field.getBytes(StandardCharsets.UTF_8)); - out.write("\r\n".getBytes(StandardCharsets.UTF_8)); - } catch (Exception ex) { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - - private void SendAuthTokens(String accessToken, String accessSecret){ - try - { - URL url = new URL(URL_ACCESS); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection)con; - http.setRequestMethod("POST"); // PUT is another valid option - http.setDoOutput(true); - - Map arguments = new HashMap<>(); - arguments.put("request_token", accessToken); - arguments.put("secret_token", accessSecret); - System.out.println("accessToken:" + accessToken + "|secret token:"+accessSecret); - - StringJoiner sj = new StringJoiner("&"); - for(Map.Entry entry : arguments.entrySet()) - sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); - byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); - int length = out.length; - - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - os.close(); - } - InputStream is = http.getInputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - byte buf[] = new byte[1024]; - int letti; - - while ((letti = is.read(buf)) > 0) - baos.write(buf, 0, letti); - - String data = new String(baos.toByteArray()); - http.disconnect(); - - } - catch(Exception ex){ - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - private void SendFinished(long Sequence_id, String accessToken) - { - try - { - URL url = new URL(URL_FINISH); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection)con; - http.setRequestMethod("POST"); // PUT is another valid option - http.setDoOutput(true); - - Map arguments = new HashMap<>(); - arguments.put("access_token", accessToken); - arguments.put("sequenceId", Long.toString(Sequence_id)); - System.out.println("accessToken:" + accessToken + "|sequenceId:"+Long.toString(Sequence_id)); - - StringJoiner sj = new StringJoiner("&"); - for(Map.Entry entry : arguments.entrySet()) - sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); - byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); - int length = out.length; - - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - os.close(); - } - InputStream is = http.getInputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - byte buf[] = new byte[1024]; - int letti; - - while ((letti = is.read(buf)) > 0) - baos.write(buf, 0, letti); - - String data = new String(baos.toByteArray()); - http.disconnect(); - - } - catch(Exception ex){ - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - private long getSequence(ImageProperties imp, String accessToken) - { - try { - URL url = new URL(URL_SEQUENCE); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection)con; - http.setRequestMethod("POST"); // PUT is another valid option - http.setDoOutput(true); - - DecimalFormat df = new DecimalFormat("#.##############"); - df.setRoundingMode(RoundingMode.CEILING); - System.out.println("Getting Sequence ID.."); - Map arguments = new HashMap<>(); - arguments.put("uploadSource", "OSVUploadr"); - arguments.put("access_token", accessToken); - arguments.put("currentCoordinate", df.format(imp.getLatitude())+ "," + df.format(imp.getLongitude())); - System.out.println("currentCoordinate:" + df.format(imp.getLatitude()) + "," + df.format(imp.getLongitude()) ); - StringJoiner sj = new StringJoiner("&"); - for(Map.Entry entry : arguments.entrySet()) - sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); - byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); - int length = out.length; - System.out.println("Sending request:" + sj.toString()); - - http.setFixedLengthStreamingMode(length); - http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - http.connect(); - try(OutputStream os = http.getOutputStream()) { - os.write(out); - os.close(); - } - System.out.println("Request Sent getting sequence response..."); - InputStream is = http.getInputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - byte buf[] = new byte[1024]; - int letti; - - while ((letti = is.read(buf)) > 0) - baos.write(buf, 0, letti); - - String data = new String(baos.toByteArray()); - int start = data.indexOf("\"osv\":{\"sequence\":{\"id\":\""); - int end = data.indexOf("\"",start+25); - System.out.println("Received request:" + data); - String sequence_id= data.substring(start+25,end); - System.out.println("Obtained Sequence ID: sequence_id"); - http.disconnect(); - return Long.parseLong(sequence_id); - - } catch (Exception ex) { - System.out.println(ex.toString()); - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - return -1; - } - - - - private void Upload_Image(ImageProperties imp, String accessToken, long Sequence_id) - { - try{ - URL url = new URL(URL_PHOTO); - URLConnection con = url.openConnection(); - HttpURLConnection http = (HttpURLConnection)con; - http.setRequestMethod("POST"); // PUT is another valid option - http.setDoOutput(true); - - String boundary = UUID.randomUUID().toString(); - byte[] boundaryBytes = boundaryBytes = ("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8); - byte[] finishBoundaryBytes = ("--" + boundary + "--").getBytes(StandardCharsets.UTF_8); - http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); - http.setRequestProperty("Accept", "*/*"); - - // Enable streaming mode with default settings - http.setChunkedStreamingMode(0); - System.out.println("Uploading:"+ imp.getFilePath()); - // Send our fields: - try(OutputStream out = http.getOutputStream()) { - // Send our header - out.write(boundaryBytes); - - //Send access token - sendField(out,"access_token",accessToken); - - // Send a seperator - out.write(boundaryBytes); - - if(imp.getCompass()>=0){ - // Send our compass - sendField(out, "headers", Double.toString(imp.getCompass())); - - // Send another seperator - out.write(boundaryBytes); - } - - // Send our coordinates - sendField(out, "coordinate", imp.getCoordinates()); - - // Send a seperator - out.write(boundaryBytes); - - // Send our sequence id - sendField(out, "sequenceId", Long.toString(Sequence_id)); - - // Send a seperator - out.write(boundaryBytes); - - // Send our sequence index - sendField(out, "sequenceIndex", Integer.toString(imp.getSequenceNumber())); - - // Send a seperator - out.write(boundaryBytes); - - - String[] tokens = imp.getFilePath().split("[\\\\|/]"); - String filename = tokens[tokens.length - 1]; - // Send our file - try(InputStream file = new FileInputStream(imp.getFilePath())) { - sendFile(out, "photo", file, filename); - } - - // Finish the request - out.write(finishBoundaryBytes); - out.close(); - } - InputStream is = http.getInputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - byte buf[] = new byte[1024]; - int letti; - - while ((letti = is.read(buf)) > 0) - baos.write(buf, 0, letti); - - String data = new String(baos.toByteArray()); - //TODO:Process returned JSON - System.out.println(data); - http.disconnect(); - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - - private void Process(String dir, String accessToken) - { - File dir_photos = new File(dir); - //filter only .jpgs - FilenameFilter fileNameFilter = new FilenameFilter() { - public boolean accept(File dir, String name) { - if (name.lastIndexOf('.') > 0) { - int lastIndex = name.lastIndexOf('.'); - String str = name.substring(lastIndex); - if (str.toLowerCase().equals(".jpg")) { - return true; - } - } - return false; - } - }; - File[] file_list = dir_photos.listFiles(fileNameFilter); - System.out.println("Pictures found:"+ String.valueOf(file_list.length)); - - System.out.println("Sorting files"); - //sort by modified time - Arrays.sort(file_list, new Comparator(){ - public int compare(File f1, File f2) - { - return Long.valueOf(Helper.getFileTime(f1)).compareTo(Helper.getFileTime(f2)); - }}); - System.out.println("End sorting"); - File f_sequence = new File(dir+"/sequence_file.txt"); - Boolean need_seq = true; - long sequence_id=-1; - System.out.println("Checking " + f_sequence.getPath() +" for sequence_file"); - if(f_sequence.exists()) - { - try - { - System.out.println("Found file, reading sequence id"); - List id = Files.readAllLines(Paths.get(f_sequence.getPath())); - if(id.size()>0) - { - sequence_id = Long.parseLong(id.get(0)); - need_seq=false; - } - } - catch(Exception ex) - { - need_seq=true; - } - } - else - { - System.out.println("Sequence file not found, will need to request new id"); - need_seq=true; - } - //TODO: Load count from file - System.out.println("Looking for count file"); - int cnt =0; - File f_cnt = new File(dir+"/count_file.txt"); - if(f_cnt.exists()) - { - System.out.println("Found count file:" + f_cnt.toString()); - try - { - List id = Files.readAllLines(Paths.get(f_cnt.getPath())); - if(id.size()>0) - { - cnt= Integer.parseInt(id.get(0)); - } - } - catch(Exception ex) - { - cnt=0; - } - } - else - { - try{ - System.out.println("Creating new count file:" + f_cnt.getPath()); - f_cnt.createNewFile(); - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, null, ex); - } - } - System.out.println("Current count at:" + String.valueOf(cnt)); - if(cnt>0){ - if(file_list.length>cnt){ - File[] tmp = new File[file_list.length-cnt]; - int local_cnt=0; - for(int i=cnt;i//GEN-BEGIN:initComponents - private void initComponents() { - - jButton1 = new javax.swing.JButton(); - listDir = new java.awt.List(); - jLabel1 = new javax.swing.JLabel(); - jButton2 = new javax.swing.JButton(); - jButton3 = new javax.swing.JButton(); - jButton4 = new javax.swing.JButton(); - jButton5 = new javax.swing.JButton(); - - jButton1.setText("Add Folder"); - jButton1.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton1.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton1.setPreferredSize(new java.awt.Dimension(123, 23)); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); - } - }); - - listDir.setMultipleMode(true); - listDir.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - listDirActionPerformed(evt); - } - }); - - jLabel1.setText("Directories"); - - jButton2.setText("Remove Duplicates"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton2ActionPerformed(evt); - } - }); - - jButton3.setText("Upload"); - jButton3.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton3.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton3.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton3ActionPerformed(evt); - } - }); - - jButton4.setText("Exit"); - jButton4.setMaximumSize(new java.awt.Dimension(123, 23)); - jButton4.setMinimumSize(new java.awt.Dimension(123, 23)); - jButton4.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton4ActionPerformed(evt); - } - }); - - jButton5.setText("Remove Folder"); - jButton5.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton5ActionPerformed(evt); - } - }); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createSequentialGroup() - .addComponent(listDir, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(24, 24, 24) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(19, 19, 19) - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(listDir, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createSequentialGroup() - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton5) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) - .addContainerGap(41, Short.MAX_VALUE)) - ); - }// //GEN-END:initComponents - - private void listDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listDirActionPerformed - // TODO add your handling code here: - }//GEN-LAST:event_listDirActionPerformed - - private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed - // TODO add your handling code here: - System.exit(0); - }//GEN-LAST:event_jButton4ActionPerformed - - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed - JFileChooser fc = new JFileChooser(); - if(!last_dir.isEmpty()){ - fc.setCurrentDirectory(new java.io.File(last_dir)); // start at application current directory - } - fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); - int returnVal = fc.showSaveDialog(this); - if(returnVal == JFileChooser.APPROVE_OPTION) { - int response = JOptionPane.showConfirmDialog(null, "Do you wish to add all immediate subfolders of this folder?", "Add subfolders?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); - if(response == JOptionPane.NO_OPTION){ - File folder = fc.getSelectedFile(); - listDir.add(folder.getPath()); - last_dir=folder.getPath(); - } - else if (response == JOptionPane.YES_OPTION) - { - //Get a list of subdirectories - String[] subDirs = fc.getSelectedFile().list(new FilenameFilter(){ - @Override - public boolean accept(File current, String name){ - return new File(current,name).isDirectory(); - } - }); - - for(String subDir: subDirs){ - listDir.add(new File(fc.getSelectedFile() + "/" +subDir).getPath()); - } - } - } - }//GEN-LAST:event_jButton1ActionPerformed - - private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed - int selectedCnt = listDir.getSelectedItems().length; - if(selectedCnt > 0) - { - for(int i = selectedCnt-1;i>=0;i--) - { - listDir.remove(listDir.getSelectedIndexes()[i]); - } - } - }//GEN-LAST:event_jButton5ActionPerformed - - private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed - String path = ca.osmcanada.osvuploadr.JFMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - String decodedPath = ""; - try{ - decodedPath = new File(URLDecoder.decode(path, "UTF-8")).getParentFile().getPath(); - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "decodePath", ex); - } - File id = new File(decodedPath + "/access_token.txt"); - String accessToken=""; - System.out.println("id_file exists:"+ id.exists()); - if(!id.exists()) - { - try{ - System.out.println("GetOSMUser"); - String token = GetOSMUser(); - Path targetPath = Paths.get("./access_token.txt"); - byte[] bytes = token.split("\\|")[0].getBytes(StandardCharsets.UTF_8); - Files.write(targetPath, bytes, StandardOpenOption.CREATE); - accessToken = token.split("\\|")[0]; - String accessSecret = token.split("\\|")[1]; - SendAuthTokens(accessToken,accessSecret); - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "GetOSMUser", ex); - } - } - else - { - try{ - List token = Files.readAllLines(Paths.get(id.getPath())); - if(token.size()>0){ - accessToken=token.get(0); //read first line - } - } - catch(Exception ex) - { - Logger.getLogger(JPMain.class.getName()).log(Level.SEVERE, "readAllLines", ex); - } - } - System.out.println("Access Token obtained from file or OSM:" + accessToken); - - //Start processing list - for(String item:listDir.getItems()){ - System.out.println("Processing folder:"+item); - Process(item,accessToken); - } - //um = new UploadManager(listDir.getItems()); - //um.start(); - - }//GEN-LAST:event_jButton3ActionPerformed - - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed - for(String item:listDir.getItems()){ - FolderCleaner fc = new FolderCleaner(item); - fc.RemoveDuplicates(); - } - }//GEN-LAST:event_jButton2ActionPerformed - - - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton jButton1; - private javax.swing.JButton jButton2; - private javax.swing.JButton jButton3; - private javax.swing.JButton jButton4; - private javax.swing.JButton jButton5; - private javax.swing.JLabel jLabel1; - private java.awt.List listDir; - // End of variables declaration//GEN-END:variables -}