| 1 | /*
|
|---|
| 2 | * Whony - Wassr Client Software
|
|---|
| 3 | *
|
|---|
| 4 | * Licensed under the MIT License
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2009 Kazushi tominaga (Seacolor)
|
|---|
| 7 | * CodeRepos (coderepos.org)
|
|---|
| 8 | *
|
|---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|---|
| 10 | * of this software and associated documentation files (the "Software"), to deal
|
|---|
| 11 | * in the Software without restriction, including without limitation the rights
|
|---|
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|---|
| 13 | * copies of the Software, and to permit persons to whom the Software is
|
|---|
| 14 | * furnished to do so, subject to the following conditions:
|
|---|
| 15 | *
|
|---|
| 16 | * The above copyright notice and this permission notice shall be included in
|
|---|
| 17 | * all copies or substantial portions of the Software.
|
|---|
| 18 | *
|
|---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|---|
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|---|
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|---|
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|---|
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|---|
| 25 | * THE SOFTWARE.
|
|---|
| 26 | *
|
|---|
| 27 | */
|
|---|
| 28 | package model.threads
|
|---|
| 29 | {
|
|---|
| 30 | import com.adobe.net.URI;
|
|---|
| 31 | import flash.filesystem.File;
|
|---|
| 32 | import flash.filesystem.FileStream;
|
|---|
| 33 | import flash.utils.ByteArray;
|
|---|
| 34 | import model.ModelLocator;
|
|---|
| 35 | import org.httpclient.http.Get;
|
|---|
| 36 | import org.httpclient.HttpClient;
|
|---|
| 37 | import org.libspark.thread.Thread;
|
|---|
| 38 | import org.libspark.thread.utils.Executor;
|
|---|
| 39 | import org.libspark.thread.utils.ParallelExecutor;
|
|---|
| 40 | import flash.filesystem.FileMode;
|
|---|
| 41 | import mx.logging.ILogger;
|
|---|
| 42 | import mx.logging.Log;
|
|---|
| 43 | import mx.utils.ObjectUtil;
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * ...
|
|---|
| 47 | * @author Kazushi Tominaga (Seacolor)
|
|---|
| 48 | */
|
|---|
| 49 | public class LocalizeImageThread extends Thread
|
|---|
| 50 | {
|
|---|
| 51 | protected const logger:ILogger = Log.getLogger(className);
|
|---|
| 52 | protected var locator:ModelLocator = ModelLocator.instance;
|
|---|
| 53 |
|
|---|
| 54 | public function LocalizeImageThread(htmlText:String, pattern:RegExp, callback:Function = null, httpClient:HttpClient = null)
|
|---|
| 55 | {
|
|---|
| 56 | logger.debug("constructor({0})", arguments);
|
|---|
| 57 | _htmlText = htmlText;
|
|---|
| 58 | _pattern = pattern;
|
|---|
| 59 | _callback = callback;
|
|---|
| 60 | _httpClient = httpClient;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | protected var _htmlText:String;
|
|---|
| 64 | protected var _pattern:RegExp;
|
|---|
| 65 | protected var _callback:Function;
|
|---|
| 66 | protected var _httpClient:HttpClient;
|
|---|
| 67 |
|
|---|
| 68 | override protected function run():void
|
|---|
| 69 | {
|
|---|
| 70 | logger.debug("method run({0})", arguments);
|
|---|
| 71 | imgUrls = _htmlText.match(_pattern);
|
|---|
| 72 |
|
|---|
| 73 | var getImgUrls:Array = new Array();
|
|---|
| 74 |
|
|---|
| 75 | if (imgUrls != null) {
|
|---|
| 76 | getImgUrls = imgUrls.filter(function(item:*, index:int, array:Array):Boolean {
|
|---|
| 77 | return !locator.emojiMap.hasOwnProperty(item);
|
|---|
| 78 | });
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | if (!getImgUrls.length) {
|
|---|
| 82 | replace();
|
|---|
| 83 | //next(replace);
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | _executor = new ParallelExecutor();
|
|---|
| 88 |
|
|---|
| 89 | for each (var imgUrl:String in getImgUrls) {
|
|---|
| 90 | _executor.addThread(new HttpClientThread(new URI(imgUrl), new Get(), 60000, _httpClient));
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | _executor.start();
|
|---|
| 94 | _executor.join();
|
|---|
| 95 |
|
|---|
| 96 | next(complete);
|
|---|
| 97 | interrupted(cancel);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | protected var imgUrls:Array;
|
|---|
| 101 |
|
|---|
| 102 | protected var _executor:Executor;
|
|---|
| 103 |
|
|---|
| 104 | protected function complete():void {
|
|---|
| 105 | logger.debug("method complete({0})", arguments);
|
|---|
| 106 |
|
|---|
| 107 | if (!LocalizeImageThread.tempDirectory) {
|
|---|
| 108 | LocalizeImageThread.tempDirectory = File.applicationStorageDirectory.resolvePath(PATH_EMOJI);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | for (var i:int = 0; i < _executor.numThreads; i++) {
|
|---|
| 112 | var thread:HttpClientThread = HttpClientThread(_executor.getThreadAt(i));
|
|---|
| 113 | var bytes:ByteArray = thread.bytes;
|
|---|
| 114 |
|
|---|
| 115 | var emoji:File = new File(LocalizeImageThread.tempDirectory.url + "/" + thread.uri.getFilename());
|
|---|
| 116 | logger.debug("emoji.exists: {0}", emoji.exists);
|
|---|
| 117 | if (!emoji.exists) {
|
|---|
| 118 | var fileStream:FileStream = new FileStream();
|
|---|
| 119 | try
|
|---|
| 120 | {
|
|---|
| 121 | fileStream.open(emoji, FileMode.WRITE);
|
|---|
| 122 | fileStream.writeBytes(bytes);
|
|---|
| 123 | } finally {
|
|---|
| 124 | fileStream.close();
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | locator.emojiMap[thread.uri.toString()] = PATH_EMOJI + emoji.name;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | replace();
|
|---|
| 131 | //next(replace);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | protected static const PATH_EMOJI:String = "emoji/";
|
|---|
| 135 |
|
|---|
| 136 | protected static var tempDirectory:File;
|
|---|
| 137 |
|
|---|
| 138 | protected function cancel():void {
|
|---|
| 139 | _executor.interrupt();
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | protected function replace():void {
|
|---|
| 143 | logger.debug("method replace({0})", arguments);
|
|---|
| 144 |
|
|---|
| 145 | //var rtnHtmlText:String = _htmlText;
|
|---|
| 146 | if (imgUrls) {
|
|---|
| 147 | for each (var url:String in imgUrls) {
|
|---|
| 148 | _htmlText = _htmlText.replace(url, locator.emojiMap[url]);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (_callback != null) _callback.apply(this, [_htmlText]);
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | } |
|---|