-
Notifications
You must be signed in to change notification settings - Fork 2
/
media-scan.js
193 lines (155 loc) · 4.51 KB
/
media-scan.js
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
angular.module("cordova.file.media",[]).service('Media',['$q','$rootScope',function ($q,$rootScope) {
//var permission =undefined;
return {
permission : function(callback)
{
var READ,WRITE=undefined;
var permissionType =undefined;
if(window.cordova)
{
/*Request Runtime PErmissions*/
cordova.plugins.diagnostic.requestRuntimePermissions(function(statuses)
{
for (var permission in statuses)
{
switch(statuses[permission])
{
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
{
// permission ="GRANTED";
log("Permission granted to use "+permission);
if(permission=="WRITE_EXTERNAL_STORAGE")
{
WRITE =true;
//defer.resolve(fileTypeCollection);
}
if(permission=="READ_EXTERNAL_STORAGE")
{
READ =true;
//defer.resolve(fileTypeCollection);
}
break;
/*Case Ends*/
}
case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
{
log("Permission to use "+permission+" has not been requested yet");
permissionType ="NOT_REQUESTED";
break;
}
case cordova.plugins.diagnostic.permissionStatus.DENIED:
{
permissionType ="DENIED";
log("Permission denied to use "+permission+" - ask again?");
break;
}
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
{
permissionType ="DENIED_ALWAYS";
log("Permission permanently denied to use "+permission+" - guess we won't be using it then!");
break;
}
}
}
/*Check Permission*/
if(WRITE && READ)
{
permissionType = "GRANTED";
callback(permissionType);
}
else
{
callback(permissionType);
}
}, function(error)
{
console.error("The following error occurred: "+error);
callback(error);
},
[
cordova.plugins.diagnostic.runtimePermission.WRITE_EXTERNAL_STORAGE,
cordova.plugins.diagnostic.runtimePermission.READ_EXTERNAL_STORAGE
]);
log("EO Permission Scan in Cordova ENV");
}
else
{
log("No Cordova Installed.Cannot Seek Permission");
callback();
}
log("Permission ENDS");
},
scan : function(url,fileType)
{
var defer = $q.defer();
if(window.cordova)
{
var fileTypeCollection = [];
url.forEach(function(element, index)
{
//requestLocalFileSystemURL
log(element);
window.resolveLocalFileSystemURL(element,onRequestFileSystem, fail);
log("Ends resolve");
});
function onRequestFileSystem(fileSystem)
{
//log(fileSystem.toURL());
log(fileSystem.toInternalURL());
var directoryReader = fileSystem.createReader();
directoryReader.readEntries(onReadEntries,fail);
} /*onRequestFile Ends*/
function onReadEntries(entries)
{
entries.forEach( function(element, index)
{
if (element.isDirectory === true)
{
// Recursive -- call back into this subdirectory
onRequestFileSystem(element);
}
else if(element.isFile == true)
{
fileType.forEach(function(type)
{
if(element.name.indexOf(type) != -1)
{
log(element.toURL());
fileTypeCollection.push(element);
defer.resolve(fileTypeCollection);
//callback(fileTypeCollection);
}
});
} /*is File ENds*/
}); /*Entries For Each Ends*/
} /*OnRead Ends*/
function fail(resp)
{
log(resp);
defer.resolve(fileTypeCollection);
} /*Fail Ends*/
}
else
{
log("No Cordova Installed.Cannot Seek File");
defer.reject();
//callback(undefined);
}
log(fileTypeCollection);
log("Before Resolve");
return defer.promise;
}, //Scan Function Ends
removeDuplicates : function(originalArray, prop)
{
var newArray = [];
var lookupObject = {};
for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for(i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
} //Remove Ends
} //Return Ends
}]);