-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathImageFile.as
More file actions
200 lines (172 loc) · 4.41 KB
/
ImageFile.as
File metadata and controls
200 lines (172 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package darkBox
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.ByteArray;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
import netManager.urlSaver.URLSaver;
import netManager.urlSaver.URLSaverEvent;
[Event(name="LOADING", type="netManager.urlSaver.URLSaverEvent")]
[Event(name="NO_INTERNET", type="netManager.urlSaver.URLSaverEvent")]
[Event(name="LOAD_COMPLETE", type="netManager.urlSaver.URLSaverEvent")]
public class ImageFile extends EventDispatcher
{
public static const TYPE_FLAT:int = 1,
TYPE_PANORAMA:int = 2,
TYPE_SPHERE:int = 3,
TYPE_VIDEO:int = 4,
TYPE_PDF:int = 5,
TYPE_WEB:int = 7,
TYPE_BINARY:int = 6;
/**Uses to catch offline file*/
private var saver:URLSaver ;
/**Uses when the application has to store offline file*/
private var onlineTarget:String,
offlineTarget:String;
/**File url*/
public var target:String;
/**Image title*/
public var title:String;
/**TYPE_FLAT:int = 1,
TYPE_PANORAMA:int = 2,
TYPE_SPHERE:int = 3,
TYPE_VIDEO:int = 4
TYPE_PDF:int = 5*/
public var type:int ;
/**You have to save this file for offline usage*/
public var storeOffline:Boolean;
private var timeOutId:uint;
public var targetBytes:ByteArray ;
public function get fullTitle():String
{
var end:String ='';
if(type!=TYPE_BINARY && type!=TYPE_PDF)
{
end = '.jpg';
}
if(title!='')
{
return title+end ;
}
else
{
return onlineTarget.substr(onlineTarget.lastIndexOf('/')+1)+end;
}
}
/**For online videos, you can pass multiple qualities starting from the best to the smallest with | seperator<br><br>
*
* TYPE_FLAT:int = 1,<br>
TYPE_PANORAMA:int = 2,<br>
TYPE_SPHERE:int = 3,<br>
TYPE_VIDEO:int = 4<br>
TYPE_PDF:int = 5<br><br>
*
* @see darkBox.ImageFile*/
public function ImageFile(Target:String='',Title:String='',Type:int=ImageFile.TYPE_FLAT,StoreOffline:Boolean=true)
{
target = Target ;
title = Title ;
type = Type ;
if(type == TYPE_PDF)
{
storeOffline = true ;
}
else
{
storeOffline = StoreOffline ;
}
}
public function isYouTube():Boolean
{
return target.toLowerCase().indexOf("youtube.com")!=-1 ;
}
public function download(timeOut:uint=0):void
{
onlineTarget = target.split('|')[0] ;
if(timeOut>0)
{
clearTimeout(timeOutId);
timeOutId = setTimeout(startDownload,timeOut);
}
else
{
startDownload();
}
}
public function qualityCount():uint
{
return target.split('|').length
}
private function startDownload():void
{
if(onlineTarget=='')
{
trace("NO File selected");
return ;
}
if(offlineTarget==null)
{
trace("Start donwload the image");
saver = new URLSaver();
saver.addEventListener(URLSaverEvent.LOAD_COMPLETE,onImageFileSaved);
saver.addEventListener(URLSaverEvent.NO_INTERNET,noNet);
saver.addEventListener(URLSaverEvent.LOADING,loadingProcess);
var extention:String ;
if(type == TYPE_PDF)
{
extention = ".pdf";
}
saver.load(onlineTarget,null,extention);
}
else
{
trace("Image is donwloaded befor");
onImageFileSaved(null,offlineTarget);
}
}
/**This will delete the temprary file*/
public function deleteFile():void
{
cansel();
if(onlineTarget!=null && onlineTarget!='')
{
saver.deletFileIfExists(onlineTarget);
}
}
protected function loadingProcess(event:URLSaverEvent):void
{
this.dispatchEvent(new URLSaverEvent(event.type,event.precent));
}
protected function noNet(event:URLSaverEvent):void
{
this.dispatchEvent(new URLSaverEvent(event.type,event.precent));
}
protected function onImageFileSaved(event:URLSaverEvent=null,downloadedFileTarget:String=null):void
{
if(event!=null)
{
target = event.offlineTarget;
targetBytes = event.loadedBytes ;
}
else
{
target = downloadedFileTarget ;
//The targetBytes had to saved once
}
offlineTarget = target ;
//Be carefull, update target befor dispatchig below event
trace("offline image target is : "+offlineTarget);
this.dispatchEvent(new URLSaverEvent(URLSaverEvent.LOAD_COMPLETE,1));
}
/**cansel downloading*/
public function cansel():void
{
clearTimeout(timeOutId);
if(saver)
{
saver.cansel()
}
}
}
}