├── .classpath
├── .gitignore
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── LICENSE
├── README.md
├── bin
└── .gitignore
├── img
└── demo.gif
├── libs
├── gson-2.6.2.jar
├── okhttp-3.2.0.jar
├── okio-1.6.0.jar
└── qiniu-java-sdk-7.1.0.jar
├── release
└── MdImageResolver_release_v1.0.jar
└── src
└── com
└── socks
├── md
└── resolver
│ ├── GsonUtil.java
│ ├── LocalProperty.java
│ ├── MainThread.java
│ ├── UploadUtil.java
│ └── Utils.java
└── test
├── ResolverTest.java
└── image.md
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.war
8 | *.ear
9 |
10 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
11 | hs_err_pid*
12 | local.properties
13 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MdImageResolver
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.5
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.5
12 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MdImageResolver
2 | ---
3 | This is a Markdown tool which can upload local image file to QiNiu service automatic.
4 |
5 | ##Feature
6 |
7 | 1. Find local image file automatic
8 | 2. Upload the local image to QiNiu service automatic
9 | 3. Replace the markdown content with url automatic
10 |
11 | ##Usage
12 |
13 | 
14 |
15 | 1. Click [hear](https://github.com/ZhaoKaiQiang/MdImageResolver/blob/master/release/MdImageResolver_release_v1.0.jar) to download the jar.
16 | 2. Execute the jar with "java -jar file_name"
17 | 3. Input your accessKey、secretKey、bucketName and domainName。
18 | 4. Choose the save mode
19 | 5. Completed !
20 |
21 |
22 |
23 | ##License
24 |
25 | ```
26 | Copyright 2015, 2016 ZhaoKaiQiang
27 |
28 | Licensed under the Apache License, Version 2.0 (the "License");
29 | you may not use this file except in compliance with the License.
30 | You may obtain a copy of the License at
31 |
32 | http://www.apache.org/licenses/LICENSE-2.0
33 |
34 | Unless required by applicable law or agreed to in writing, software
35 | distributed under the License is distributed on an "AS IS" BASIS,
36 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 | See the License for the specific language governing permissions and
38 | limitations under the License.
39 | ```
40 |
41 | ##Contact & Help
42 | - Weibo:[http://weibo.com/zhaokaiqiang1992](http://weibo.com/zhaokaiqiang1992)
43 | - Blog:[http://kaizige.vip/](http://kaizige.vip/)
44 | - Email:[zhaokaiqiang@gmail.com](zhaokaiqiang@gmail.com)
--------------------------------------------------------------------------------
/bin/.gitignore:
--------------------------------------------------------------------------------
1 | /com
2 |
--------------------------------------------------------------------------------
/img/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/img/demo.gif
--------------------------------------------------------------------------------
/libs/gson-2.6.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/libs/gson-2.6.2.jar
--------------------------------------------------------------------------------
/libs/okhttp-3.2.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/libs/okhttp-3.2.0.jar
--------------------------------------------------------------------------------
/libs/okio-1.6.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/libs/okio-1.6.0.jar
--------------------------------------------------------------------------------
/libs/qiniu-java-sdk-7.1.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/libs/qiniu-java-sdk-7.1.0.jar
--------------------------------------------------------------------------------
/release/MdImageResolver_release_v1.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhaoKaiQiang/MdImageResolver/b37b6f712b9e58564bfbb83c361505ac0aeded4a/release/MdImageResolver_release_v1.0.jar
--------------------------------------------------------------------------------
/src/com/socks/md/resolver/GsonUtil.java:
--------------------------------------------------------------------------------
1 | package com.socks.md.resolver;
2 |
3 | import com.google.gson.Gson;
4 |
5 | public class GsonUtil {
6 |
7 | private static Gson mGson;
8 |
9 | private GsonUtil() {
10 | }
11 |
12 | public static Gson getInstance() {
13 | if (mGson == null) {
14 | synchronized (GsonUtil.class) {
15 | if (mGson == null) {
16 | mGson = new Gson();
17 | }
18 | }
19 | }
20 | return mGson;
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/socks/md/resolver/LocalProperty.java:
--------------------------------------------------------------------------------
1 | package com.socks.md.resolver;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.File;
5 | import java.io.FileWriter;
6 |
7 | public class LocalProperty {
8 |
9 | private static final String LOCAL_PROPERTIES = "local.properties";
10 |
11 | private String accessKey;
12 | private String secretKey;
13 | private String bucketName;
14 | private String domainName;
15 |
16 | public static LocalProperty getLocalProperty() {
17 | File localProperties = new File(LOCAL_PROPERTIES);
18 | if (localProperties.exists() && localProperties.canRead()) {
19 | String properties = Utils.getStringFromFile(localProperties,
20 | "UTF-8");
21 | return (LocalProperty) GsonUtil.getInstance().fromJson(properties,
22 | LocalProperty.class);
23 | }
24 | return null;
25 | }
26 |
27 | public static LocalProperty getLocalPropertyByConsole(){
28 | String accessKey = Utils
29 | .readDataFromConsole("Please input your access key:\n");
30 | String secretKey = Utils
31 | .readDataFromConsole("Please input your secret key:\n");
32 | String bucketName = Utils
33 | .readDataFromConsole("Please input your bucket name:\n");
34 | String domainName = Utils
35 | .readDataFromConsole("Please input your domain name:\n");
36 |
37 | LocalProperty localProperty = new LocalProperty();
38 | localProperty.setAccessKey(accessKey);
39 | localProperty.setSecretKey(secretKey);
40 | localProperty.setBucketName(bucketName);
41 | localProperty.setDomainName(domainName);
42 | localProperty.saveLocalProperty();
43 | return localProperty;
44 | }
45 |
46 | public void saveLocalProperty() {
47 | File localProperties = new File(LOCAL_PROPERTIES);
48 |
49 | if (!localProperties.exists()) {
50 | String jsonString = GsonUtil.getInstance().toJson(this);
51 | try {
52 | BufferedWriter writer = new BufferedWriter(new FileWriter(
53 | localProperties));
54 | writer.write(jsonString);
55 | writer.flush();
56 | writer.close();
57 | } catch (Exception e) {
58 | e.printStackTrace();
59 | }
60 | } else {
61 | Utils.println("File exits ! " + localProperties.getAbsolutePath());
62 | }
63 | }
64 |
65 | public String getImageUrl(String fileName) {
66 | return "http://" + getDomainName().trim() + "/" + fileName;
67 | }
68 |
69 | public String getAccessKey() {
70 | return accessKey;
71 | }
72 |
73 | public void setAccessKey(String accessKey) {
74 | this.accessKey = accessKey;
75 | }
76 |
77 | public String getSecretKey() {
78 | return secretKey;
79 | }
80 |
81 | public void setSecretKey(String secretKey) {
82 | this.secretKey = secretKey;
83 | }
84 |
85 | public String getBucketName() {
86 | return bucketName;
87 | }
88 |
89 | public void setBucketName(String bucketName) {
90 | this.bucketName = bucketName;
91 | }
92 |
93 | public String getDomainName() {
94 | return domainName;
95 | }
96 |
97 | public void setDomainName(String domainName) {
98 | this.domainName = domainName;
99 | }
100 |
101 | public String toString() {
102 | return "LocalProperty [accessKey=" + accessKey + ", secretKey="
103 | + secretKey + ", bucketName=" + bucketName + ", domainName="
104 | + domainName + "]";
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/src/com/socks/md/resolver/MainThread.java:
--------------------------------------------------------------------------------
1 | package com.socks.md.resolver;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 |
6 | import com.qiniu.http.Response;
7 |
8 | public class MainThread {
9 |
10 | private static final int MODE_COVER = 1;
11 | private static final int MODE_BACKUP = 2;
12 |
13 | private static UploadUtil mUploadDemo;
14 | private static LocalProperty mLocalProperty;
15 |
16 | public static void main(String[] args) throws Exception {
17 |
18 | mLocalProperty = LocalProperty.getLocalProperty();
19 |
20 | if (mLocalProperty == null) {
21 | mLocalProperty = LocalProperty.getLocalPropertyByConsole();
22 | }
23 |
24 | mUploadDemo = new UploadUtil(mLocalProperty.getAccessKey(),
25 | mLocalProperty.getSecretKey(), mLocalProperty.getBucketName());
26 |
27 | String filePath = Utils
28 | .readDataFromConsole("Please drag your Markdown file to this window \n");
29 |
30 | int mode = 0;
31 | while (mode != MODE_COVER && mode != MODE_BACKUP) {
32 | String modeString = Utils
33 | .readDataFromConsole(
34 | "Please choose a save mode :\n 1.Coverage Mode.The source file will be overwritten. \n 2.BackUp Mode. Create a new back up file.\n")
35 | .trim();
36 | try {
37 | mode = Integer.valueOf(modeString);
38 | } catch (Exception e) {
39 | mode = 0;
40 | Utils.println("Please input 1 or 2 to choose save mode !");
41 | }
42 | }
43 |
44 | File targetFile = new File(filePath);
45 |
46 | if (!targetFile.exists() || !targetFile.canWrite()) {
47 | Utils.println("Error ! File not found or can't be write !");
48 | return;
49 | }
50 |
51 | Utils.println("Start parse file :" + targetFile.getAbsolutePath()
52 | + "\n");
53 | String blogString = Utils.getStringFromFile(targetFile, "UTF-8");
54 |
55 | File imgFile;
56 | int successCount = 0;
57 | int failedCount = 0;
58 |
59 | ArrayList tagsArrayList = Utils.getImageTags(blogString);
60 |
61 | if (tagsArrayList.size() > 0) {
62 |
63 | for (int i = 0; i < tagsArrayList.size(); i++) {
64 | String imageTag = tagsArrayList.get(i);
65 | String imgFilePath = Utils.getFilePathFromImageTag(imageTag);
66 | String altString = Utils.getAltFromImageTag(imageTag);
67 | Utils.println("Find Local image :" + imgFilePath + "\n");
68 | imgFile = new File(imgFilePath);
69 |
70 | if (!imgFile.exists() || !imgFile.canRead()) {
71 | Utils.println("File not found or can't be write !\n");
72 | failedCount++;
73 | continue;
74 | }
75 |
76 | Utils.println("Start upload " + imgFile.getName()
77 | + " to QiNiu service...\n");
78 | Response response = mUploadDemo.upload(
79 | imgFile.getAbsolutePath(), imgFile.getName());
80 | if (response.isOK()) {
81 | String qiniuUrl = mLocalProperty.getImageUrl(imgFile
82 | .getName());
83 | Utils.println("Upload successful ! The url of image is "
84 | + qiniuUrl + "\n");
85 | blogString = blogString.replace(imageTag,
86 | Utils.getImageTagByUrl(altString, qiniuUrl));
87 | successCount++;
88 | } else {
89 | failedCount++;
90 | Utils.println("Error ! " + response.error);
91 | }
92 |
93 | }
94 |
95 | switch (mode) {
96 | case MODE_COVER:
97 | Utils.writeStringToFile(blogString, targetFile);
98 | Utils.println("Save as " + targetFile.getAbsolutePath() + "\n");
99 | break;
100 | case MODE_BACKUP:
101 | String fileName = Utils.getBackUpFileName(targetFile.getName());
102 | File backupFile = new File(targetFile.getParentFile(), fileName);
103 | Utils.writeStringToFile(blogString, backupFile);
104 | Utils.println("Save as " + backupFile.getAbsolutePath() + "\n");
105 | break;
106 | default:
107 | throw new IllegalArgumentException("ERROR MODE " + mode);
108 | }
109 |
110 | Utils.println("Upload image completed ! Success:" + successCount
111 | + " Failed:" + failedCount);
112 | } else {
113 | Utils.println("Didn't find any local image tag.");
114 | }
115 | System.exit(1);
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/src/com/socks/md/resolver/UploadUtil.java:
--------------------------------------------------------------------------------
1 | package com.socks.md.resolver;
2 |
3 | import java.io.IOException;
4 |
5 | import com.qiniu.common.QiniuException;
6 | import com.qiniu.http.Response;
7 | import com.qiniu.storage.UploadManager;
8 | import com.qiniu.util.Auth;
9 |
10 | public class UploadUtil {
11 |
12 | private String mAccessKey;
13 | private String mSecertKey;
14 | private String mBucketName;
15 | private Auth mAuth;
16 | private UploadManager mUploadManager;
17 |
18 | public UploadUtil(String accessKey, String secertKEY, String bucketName) {
19 | mAccessKey = accessKey;
20 | mSecertKey = secertKEY;
21 | mBucketName = bucketName;
22 | mAuth = Auth.create(mAccessKey, mSecertKey);
23 | mUploadManager = new UploadManager();
24 | }
25 |
26 | private String getUpToken() {
27 | return mAuth.uploadToken(mBucketName);
28 | }
29 |
30 | public Response upload(String filePath, String fileName) throws IOException {
31 | try {
32 | return mUploadManager.put(filePath, fileName, getUpToken());
33 | } catch (QiniuException e) {
34 | return e.response;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/com/socks/md/resolver/Utils.java:
--------------------------------------------------------------------------------
1 | package com.socks.md.resolver;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.FileWriter;
7 | import java.io.IOException;
8 | import java.io.InputStreamReader;
9 | import java.util.ArrayList;
10 | import java.util.regex.Matcher;
11 | import java.util.regex.Pattern;
12 |
13 | public class Utils {
14 |
15 | private static final Pattern LOCAL_IMAGE_PATTERN = Pattern
16 | .compile("!\\[.*]\\([\\w-_/]+.[png]*[jpg]*[PNG]*[jpeg]*[gif]*\\)");
17 |
18 | private static final Pattern IMAGE_ALT_PATTERN = Pattern.compile("!\\[.*]");
19 |
20 | /**
21 | * @return all the local image tag in blogString
22 | */
23 | public static ArrayList getImageTags(String blogString) {
24 | ArrayList tags = new ArrayList();
25 | Matcher matcher = LOCAL_IMAGE_PATTERN.matcher(blogString);
26 | while (matcher.find()) {
27 | String imageTag = matcher.group();
28 | tags.add(imageTag);
29 | }
30 | return tags;
31 | }
32 |
33 | public static String getBackUpFileName(String mdFileName) {
34 | String[] strs = mdFileName.split("\\.");
35 | if (strs.length != 2) {
36 | return mdFileName;
37 | }
38 | return strs[0] + "_bak." + strs[1];
39 | }
40 |
41 | public static String getAltFromImageTag(String imageTag) {
42 | Matcher matcher = IMAGE_ALT_PATTERN.matcher(imageTag);
43 | if (matcher.find()) {
44 | return matcher.group().replace("![", "").replace("]", "");
45 | } else {
46 | return "";
47 | }
48 | }
49 |
50 | public static void writeStringToFile(String content, File targetFile)
51 | throws Exception {
52 | FileWriter writer = new FileWriter(targetFile);
53 | writer.write(content);
54 | writer.flush();
55 | writer.close();
56 | }
57 |
58 | public static String getImageTagByUrl(String alt, String url) {
59 | return "";
60 | }
61 |
62 | public static String getFilePathFromImageTag(String localImageTag) {
63 | return localImageTag.replace(getAltFromImageTag(localImageTag), "")
64 | .replace(".replace(")", "");
65 | }
66 |
67 | public static String getStringFromFile(File file, String encode) {
68 | StringBuilder sb = new StringBuilder();
69 | try {
70 | InputStreamReader read = new InputStreamReader(new FileInputStream(
71 | file), encode);
72 | BufferedReader bufferedReader = new BufferedReader(read);
73 | String lineTxt = null;
74 | while ((lineTxt = bufferedReader.readLine()) != null) {
75 | sb.append(lineTxt).append("\n");
76 | }
77 | read.close();
78 | } catch (Exception e) {
79 | e.printStackTrace();
80 | return "";
81 | }
82 | return sb.toString();
83 | }
84 |
85 | public static String getFileNameFromImageTag(String localImageTag) {
86 | String[] strArray = localImageTag.split("/");
87 | return strArray[strArray.length - 1].replace(")", "");
88 | }
89 |
90 | public static void println(String message) {
91 | System.out.println(message);
92 | }
93 |
94 | public static String readDataFromConsole(String prompt) {
95 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
96 | String str = null;
97 | try {
98 | System.out.print(prompt);
99 | str = br.readLine();
100 |
101 | } catch (IOException e) {
102 | e.printStackTrace();
103 | }
104 | return str.trim();
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/com/socks/test/ResolverTest.java:
--------------------------------------------------------------------------------
1 | package com.socks.test;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 |
6 | import org.junit.Test;
7 | import com.socks.md.resolver.Utils;
8 |
9 | public class ResolverTest {
10 |
11 | @Test
12 | public void testParseImageTag() throws Exception {
13 | File targetFile = new File(
14 | "/Users/zhao/Desktop/MdImageResolver/src/com/socks/test",
15 | "image.md");
16 | String blogString = Utils.getStringFromFile(targetFile, "UTF-8");
17 | ArrayList tagsArrayList = Utils.getImageTags(blogString);
18 | for (int i = 0; i < tagsArrayList.size(); i++) {
19 | Utils.println(tagsArrayList.get(i));
20 | }
21 | }
22 |
23 | @Test
24 | public void testFindAlt() throws Exception {
25 | File targetFile = new File(
26 | "/Users/zhao/Desktop/MdImageResolver/src/com/socks/test",
27 | "image.md");
28 | String blogString = Utils.getStringFromFile(targetFile, "UTF-8");
29 | ArrayList tagsArrayList = Utils.getImageTags(blogString);
30 |
31 | for (int i = 0; i < tagsArrayList.size(); i++) {
32 | String imageTag = tagsArrayList.get(i);
33 | String imgFilePath = Utils.getFilePathFromImageTag(imageTag);
34 | String altString = Utils.getAltFromImageTag(imageTag);
35 | Utils.println("Find Local image path is :\n" + imgFilePath
36 | + "\nImage alt is " + altString);
37 | Utils.println("New image tag is "
38 | + Utils.getImageTagByUrl(altString,
39 | "http://www.google.com/") + "\n");
40 | }
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/socks/test/image.md:
--------------------------------------------------------------------------------
1 | 
2 | 
3 | 
4 | 
5 | 
--------------------------------------------------------------------------------