OSDN Git Service

Add utility to pull image from an HTML URL and create a resource.
authorRandy Baumgarte <randy@fbn.cx>
Wed, 21 Dec 2011 12:11:13 +0000 (07:11 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Wed, 21 Dec 2011 12:11:13 +0000 (07:11 -0500)
src/cx/fbn/nevernote/utilities/ImageFetcher.java [new file with mode: 0644]

diff --git a/src/cx/fbn/nevernote/utilities/ImageFetcher.java b/src/cx/fbn/nevernote/utilities/ImageFetcher.java
new file mode 100644 (file)
index 0000000..81954cb
--- /dev/null
@@ -0,0 +1,166 @@
+/*\r
+ * This file is part of NixNote \r
+ * Copyright 2009 Randy Baumgarte\r
+ * \r
+ * This file may be licensed under the terms of of the\r
+ * GNU General Public License Version 2 (the ``GPL'').\r
+ *\r
+ * Software distributed under the License is distributed\r
+ * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
+ * express or implied. See the GPL for the specific language\r
+ * governing rights and limitations.\r
+ *\r
+ * You should have received a copy of the GPL along with this\r
+ * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
+ * or write to the Free Software Foundation, Inc.,\r
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
+ *\r
+ */\r
+\r
+package cx.fbn.nevernote.utilities;\r
+\r
+//Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.\r
+//You may study, use, modify, and distribute this example for any purpose.\r
+//This example is provided WITHOUT WARRANTY either expressed or implied.\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+import java.net.URLConnection;\r
+import java.security.MessageDigest;\r
+import java.security.NoSuchAlgorithmException;\r
+\r
+import com.evernote.edam.type.Data;\r
+import com.evernote.edam.type.Resource;\r
+import com.evernote.edam.type.ResourceAttributes;\r
+import com.trolltech.qt.core.QUuid;\r
+\r
+import cx.fbn.nevernote.sql.DatabaseConnection;\r
+\r
+public class ImageFetcher {\r
+       \r
+       private Resource newResource;\r
+       private final ApplicationLogger logger;\r
+       private final DatabaseConnection conn;\r
+       private final String noteGuid;\r
+       \r
+       \r
+       public ImageFetcher(DatabaseConnection c, ApplicationLogger l, String g) {\r
+               newResource = new Resource();\r
+               logger = l;\r
+               conn = c;\r
+               noteGuid = g;\r
+       }\r
+       \r
+\r
+ // Get the contents of a URL and return it as an image\r
+ public boolean fetch(String address) \r
+     throws MalformedURLException, IOException \r
+ {\r
+        URL u = new URL(address);\r
+        URLConnection uc = u.openConnection();\r
+        String contentType = uc.getContentType();\r
+        int contentLength = uc.getContentLength();\r
+        if (contentType.startsWith("text/") || contentLength == -1) {\r
+            throw new IOException("This is not a binary file.");\r
+        }\r
+        InputStream raw = uc.getInputStream();\r
+        InputStream in = new BufferedInputStream(raw);\r
+        byte[] data = new byte[contentLength];\r
+        int bytesRead = 0;\r
+        int offset = 0;\r
+        while (offset < contentLength) {\r
+          bytesRead = in.read(data, offset, data.length - offset);\r
+          if (bytesRead == -1)\r
+            break;\r
+          offset += bytesRead;\r
+        }\r
+        in.close();\r
+\r
+        if (offset != contentLength) {\r
+          throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");\r
+        }\r
+       \r
+        newResource = createResource(data, address);\r
+        if (newResource != null)\r
+                return true;\r
+        else\r
+                return false;\r
+ }\r
\r
\r
+       // Convert the binary data to a resource\r
+       private Resource createResource(byte[] fileData, String address) {\r
+               logger.log(logger.EXTREME, "Inside create resource");\r
+               //These two lines are added to handle odd characters in the name like #.  Without it\r
+               // toLocalFile() chokes and returns the wrong name.\r
+       MessageDigest md;\r
+       try {\r
+               logger.log(logger.EXTREME, "Generating MD5");\r
+               md = MessageDigest.getInstance("MD5");\r
+               md.update(fileData);\r
+               byte[] hash = md.digest();\r
+  \r
+               Resource r = new Resource();\r
+               r.setGuid(QUuid.createUuid().toString().replace("}", "").replace("{", ""));\r
+               r.setNoteGuid(noteGuid);\r
+               r.setMime("image/" +address.substring(address.lastIndexOf(".")+1));\r
+               r.setActive(true);\r
+               r.setUpdateSequenceNum(0);\r
+               r.setWidth((short) 0);\r
+               r.setHeight((short) 0);\r
+               r.setDuration((short) 0);\r
+                               \r
+               Data d = new Data();\r
+               d.setBody(fileData);\r
+               d.setBodyIsSet(true);\r
+               d.setBodyHash(hash);\r
+               d.setBodyHashIsSet(true);\r
+               r.setData(d);\r
+               d.setSize(fileData.length);\r
+               \r
+               int fileNamePos = address.lastIndexOf(File.separator);\r
+               if (fileNamePos == -1)\r
+                       fileNamePos = address.lastIndexOf("/");\r
+                       String fileName = address.substring(fileNamePos+1);\r
+               ResourceAttributes a = new ResourceAttributes();\r
+               a.setAltitude(0);\r
+               a.setAltitudeIsSet(false);\r
+               a.setLongitude(0);\r
+               a.setLongitudeIsSet(false);\r
+               a.setLatitude(0);\r
+               a.setLatitudeIsSet(false);\r
+               a.setCameraMake("");\r
+               a.setCameraMakeIsSet(false);\r
+               a.setCameraModel("");\r
+               a.setCameraModelIsSet(false);\r
+               a.setAttachment(false);\r
+               a.setAttachmentIsSet(true);\r
+               a.setClientWillIndex(false);\r
+               a.setClientWillIndexIsSet(true);\r
+               a.setRecoType("");\r
+               a.setRecoTypeIsSet(false);\r
+               a.setSourceURL(fileName);\r
+               a.setSourceURLIsSet(true);\r
+               a.setTimestamp(0);\r
+               a.setTimestampIsSet(false);\r
+               a.setFileName(fileName);\r
+               a.setFileNameIsSet(true);\r
+               r.setAttributes(a);\r
+               \r
+               //conn.getNoteTable().noteResourceTable.saveNoteResource(r, true);\r
+               logger.log(logger.EXTREME, "Resource created");\r
+               \r
+               // Now write it out so someone else can look at it\r
+               \r
+               return r;\r
+       } catch (NoSuchAlgorithmException e1) {\r
+               e1.printStackTrace();\r
+               }\r
+       return null;\r
+       }\r
+\r
+}
\ No newline at end of file