forked from scripterkaran/image-saver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSaver.js
More file actions
executable file
·91 lines (89 loc) · 4.1 KB
/
imageSaver.js
File metadata and controls
executable file
·91 lines (89 loc) · 4.1 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
angular.module('image-saver', [])
.directive('imageSaver', function (FileService, $cordovaNetwork,$timeout) {
function link(scope, element, attr) {
var fileName = attr.name.split('//').pop().split('/').pop();
if ($cordovaNetwork.isOnline()) {
FileService.CheckThenDownloadFile(attr.name, fileName, attr.directory)
.then(function (res) {
element.attr('src', res);
$timeout(function () {
scope.$apply()
});
}, function (err) {
//error
})
}else{
FileService.getOfflineData(fileName, attr.directory).then(function (res) {
element.attr('src',res);
$timeout(function () {
scope.$apply()
})
})
}
}
return {
restrict: 'A',
scope: {
name: '@',
directory: '@'
},
link: link
}
})
.factory('FileService', function ($q, $cordovaFile, $cordovaFileTransfer) {
var StorageDirectory = cordova.file.cacheDirectory;
function DownloadFileAsync(url,StorageDirectory,directory,file,options,AllowAllHost){
var q = $q.defer();
$cordovaFileTransfer.download(url,StorageDirectory+directory+'/'+file,options,AllowAllHost).then(function () {
var path = StorageDirectory+directory+'/'+file;
q.resolve(path)
}, function (err) {
q.reject(err);
});
return q.promise
}
return {
CheckThenDownloadFile: function (url, file, directory) {
var q = $q.defer();
$cordovaFile.checkDir(cordova.file.cacheDirectory, directory).then(function () {
$cordovaFile.checkFile(StorageDirectory+directory+'/',file).then(function () {
var path = StorageDirectory+directory+'/'+file;
q.resolve(path)
}, function () {
//download file if not exist
var options = {create : true, exclusive: false};
DownloadFileAsync(url,StorageDirectory,directory,file,options,true).then(function (res) {
q.resolve(res); //resolving path of local file that downloaded
}, function (err) {
//error Download
q.reject(err)
})
})
}, function () { //create dir (for first time)
var options = {create : true, exclusive: false};
$cordovaFile.createDir(StorageDirectory,directory,true).then(function () {
DownloadFileAsync(url,StorageDirectory,directory,file,options,true).then(function (res) {
q.resolve(res)
}, function () {
q.reject()
})
})
});
return q.promise
},
getOfflineData: function (file, directory) {
var q = $q.defer();
$cordovaFile.checkDir(StorageDirectory, directory).then(function () {
$cordovaFile.checkFile(StorageDirectory+directory+'/', file).then(function () { //file exists
var path = StorageDirectory+directory+'/'+file;
q.resolve(path)
}, function (err) {
q.reject(err)
})
}, function (err) {
q.reject(err)
});
return q.promise
}
}
});