22
33
44import com .luciad .imageio .webp .WebPWriteParam ;
5- import com .lzq .api .pojo .Content ;
65import com .lzq .api .pojo .Example ;
7- import com .lzq .api .service .ContentService ;
86import com .lzq .api .service .ExampleService ;
97import com .qiniu .util .StringUtils ;
108import lombok .extern .slf4j .Slf4j ;
1412import org .openqa .selenium .chrome .ChromeDriver ;
1513import org .openqa .selenium .chrome .ChromeOptions ;
1614import org .springframework .beans .factory .annotation .Value ;
15+ import org .springframework .scheduling .annotation .Async ;
1716import org .springframework .stereotype .Component ;
1817
1918import javax .imageio .IIOImage ;
2423import java .io .File ;
2524import java .io .FileOutputStream ;
2625import java .io .IOException ;
27- import java .util .UUID ;
26+ import java .util .Date ;
2827import java .util .concurrent .TimeUnit ;
2928
3029@ Slf4j
@@ -39,8 +38,6 @@ public class ExampleUtils {
3938
4039 public static String BUCKET ;
4140
42- private static final char [] _UU64 = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" .toCharArray ();
43-
4441 @ Value ("${resources.InitHtml}" )
4542 public void setInitHtml (String initHtml ) {
4643 INIT_HTML = initHtml ;
@@ -86,7 +83,7 @@ public static String screenshot(String username, String filename) throws IOExcep
8683 Dimension dim = new Dimension (winWidth , winHeight );
8784 broswer .manage ().window ().setSize (dim );
8885 //等待1秒,
89- broswer .manage ().timeouts ().implicitlyWait (2 , TimeUnit .SECONDS );
86+ broswer .manage ().timeouts ().implicitlyWait (1 , TimeUnit .SECONDS );
9087 //打开url
9188 broswer .get ("http://localhost:8090/" + username + "/" + filename + ".html" );
9289 //截图
@@ -110,26 +107,22 @@ public static String screenshot(String username, String filename) throws IOExcep
110107 * 保存实例
111108 *
112109 * @param example
113- * @param exampleContent
114110 * @param content 编译后的内容
115111 * @param exampleService
116- * @param contentService
112+ * @param file
117113 * @return
118114 * @throws IOException
119115 */
120- public static Boolean SaveExampleContent (Example example , Content exampleContent , String content , ExampleService exampleService , ContentService contentService ) throws IOException {
121- //获取html文件路劲
122- //实例内容和实例进行绑定
123- exampleContent .setExampleId (example .getExampleId ());
124- String file = FILE_LOCATION + example .getUsername () + "/" + example .getFileName () + ".html" ;
125- log .info (file );
116+ @ Async
117+ public void SaveExampleContent (Example example , String content , ExampleService exampleService ,String file ) throws IOException {
118+ log .info ("开始截图-------" +new Date (System .currentTimeMillis ()));
119+ //获取文件输出流
126120 FileOutputStream fos = new FileOutputStream (new File (file ));
127121 String screenshot = null ;
128122 Boolean bol = false ;
129123 try {
130124 //包编译后的html内容覆盖原来的内容
131125 fos .write (content .getBytes ("UTF-8" ));
132- // fos.write(content.getBytes("GBK")) ;
133126 //第一次保存时生成图片
134127 if (StringUtils .isNullOrEmpty (example .getImg ())) {
135128 //截图后进行保存
@@ -145,20 +138,15 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
145138 }
146139 //更新实例
147140 bol = exampleService .update (example );
148- //修改实例内容
149- bol = contentService .updateContent (exampleContent );
150- //当表无该数据时插入数据
151- if (!bol ) {
152- //第一次保存时在表中添加实例内容
153- bol = contentService .addContent (exampleContent );
154- }
155- return bol ;
141+ log .info ("截图成功" );
156142 } catch (IOException e ) {
157143 e .printStackTrace ();
158- return bol ;
144+ log .info ("截图失败" );
145+ // return bol;
159146 } finally {
160147 log .info ("上传关闭流-----------------------" );
161148 fos .close ();
149+ log .info ("结束截图-------" +new Date (System .currentTimeMillis ()));
162150 }
163151 }
164152
@@ -190,30 +178,4 @@ public static void convertWebp(File oldfile, File newfile) throws IOException {
190178 }
191179
192180
193- //生成22为uuid
194- public static String getUUid () {
195- UUID uuid = UUID .randomUUID ();
196- int index = 0 ;
197- char [] cs = new char [22 ];
198- long L = uuid .getMostSignificantBits ();
199- long R = uuid .getLeastSignificantBits ();
200- long mask = 63 ;
201- // 从L64位取10次,每次取6位
202- for (int off = 58 ; off >= 4 ; off -= 6 ) {
203- long hex = (L & (mask << off )) >>> off ;
204- cs [index ++] = _UU64 [(int ) hex ];
205- }
206- // 从L64位取最后的4位 + R64位头2位拼上
207- int l = (int ) (((L & 0xF ) << 2 ) | ((R & (3 << 62 )) >>> 62 ));
208- cs [index ++] = _UU64 [l ];
209- // 从R64位取10次,每次取6位
210- for (int off = 56 ; off >= 2 ; off -= 6 ) {
211- long hex = (R & (mask << off )) >>> off ;
212- cs [index ++] = _UU64 [(int ) hex ];
213- }
214- // 剩下的两位最后取
215- cs [index ++] = _UU64 [(int ) (R & 3 )];
216- // 返回字符串
217- return new String (cs );
218- }
219181}
0 commit comments