1- var APPLICATION_ID = '' ;
2- var SECRET_KEY = '' ;
3- var VERSION = 'v1' ;
4-
5- var DEVICE_ID = "fileServiceTest" ;
6- var TEST_FOLDER = "testFolder" ;
7- var files ;
8-
9- if ( ! APPLICATION_ID || ! SECRET_KEY || ! VERSION )
10- alert ( "Missing application ID and secret key arguments. Login to Backendless Console, select your app and get the ID and key from the Manage > App Settings screen. Copy/paste the values into the Backendless.initApp call located in FilesExample.js" ) ;
11-
12- $ ( ) . ready ( function ( )
13- {
14- init ( ) ;
15- } ) ;
16-
17- function init ( ) {
18- $ ( '.carousel' ) . carousel ( { interval : false } ) ;
19- Backendless . initApp ( APPLICATION_ID , SECRET_KEY , VERSION ) ;
20- }
1+ ( function ( Backendless , $ ) {
2+
3+ var APPLICATION_ID = '' ;
4+ var SECRET_KEY = '' ;
5+ var VERSION = 'v1' ;
6+
7+ var DEVICE_ID = 'fileServiceTest' ;
8+ var TEST_FOLDER = 'testFolder' ;
9+ var files = [ ] ;
10+
11+ if ( ! APPLICATION_ID || ! SECRET_KEY || ! VERSION )
12+ alert ( "Missing application ID and secret key arguments. Login to Backendless Console, select your app and get the ID and key from the Manage > App Settings screen. Copy/paste the values into the Backendless.initApp call located in files-example.js" ) ;
13+
14+ function init ( ) {
15+ $ ( '.carousel' ) . carousel ( { interval : false } ) ;
16+
17+ Backendless . enablePromises ( ) ;
18+ Backendless . initApp ( APPLICATION_ID , SECRET_KEY , VERSION ) ;
19+
20+ initHandlers ( ) ;
21+ }
22+
23+ function initHandlers ( ) {
24+ $ ( '#files' ) . on ( 'change' , handleFileSelect ) ;
25+ $ ( '.thumbnails' ) . on ( 'click' , '.thumbnail' , onClickFileItem ) ;
26+ $ ( '#browse-uploaded-link' ) . on ( 'click' , refreshItemsList ) ;
27+ $ ( '#upload-btn' ) . on ( 'click' , uploadFile ) ;
28+ $ ( '#delete-btn' ) . on ( 'click' , deleteSelectedFiles ) ;
29+ }
30+
31+ function protectXSS ( val ) {
32+ return val . replace ( / ( < | > | \/ ) / g, function ( match ) {
33+ return match == '<' ? '<' : match == '>' ? '>' : '/' ;
34+ } ) ;
35+ }
36+
37+ function handleFileSelect ( evt ) {
38+ var output = [ ] ;
39+ for ( var i = 0 , f ; f = evt . target . files [ i ] ; i ++ ) {
40+ output . push ( '<li><strong>' , protectXSS ( f . name ) , '</strong> (' , f . type || 'n/a' , ') - ' ,
41+ f . size , ' bytes, last modified: ' ,
42+ f . lastModifiedDate ? f . lastModifiedDate . toLocaleDateString ( ) : 'n/a' ,
43+ '</li>' ) ;
44+ files . push ( f )
45+ }
46+ $ ( '#list' ) . append ( '<ul>' + output . join ( '' ) + '</ul>' ) ;
47+ $ ( '#files' ) . val ( '' ) ;
48+ }
49+
50+ function FileItem ( ) {
51+ this . url = '' ;
52+ this . deviceId = DEVICE_ID ;
53+ }
54+
55+ function createNewItem ( fileUrl ) {
56+ var record = new FileItem ( ) ;
57+ record . url = fileUrl ;
58+
59+ return Backendless . Persistence . of ( FileItem ) . save ( record ) ;
60+ }
61+
62+ function deleteItem ( id ) {
63+ return Backendless . Persistence . of ( FileItem ) . remove ( id ) ;
64+ }
65+
66+ function onClickFileItem ( ) {
67+ $ ( this ) . toggleClass ( 'selectedThumbnail' ) ;
68+ }
69+
70+ function refreshItemsList ( ) {
71+ getItemsFromPersistance ( ) . then ( function ( result ) { renderItems ( result . data ) ; } ) ;
72+ }
73+
74+ function renderItems ( items ) {
75+ $ ( '.thumbnails' ) . empty ( ) ;
76+
77+ $ . each ( items , function ( index , value ) {
78+ var name = getRelativeFileName ( value . url ) ;
79+ var li = (
80+ '<li class="span4">' +
81+ '<div class="thumbnail">' +
82+ '<img class="dataPicture" id="' + value . objectId + '" src="' + value . url + '" alt=""/>' +
83+ '<div align="center">' +
84+ '<a href="' + value . url + '" >' + decodeURIComponent ( protectXSS ( name ) ) + '</a>' +
85+ '</div>' +
86+ '</div>' +
87+ '</li>'
88+ ) ;
89+
90+ $ ( '.thumbnails' ) . append ( li ) ;
91+ } ) ;
92+ }
93+
94+ function getRelativeFileName ( str ) {
95+ var rest = str . substring ( 0 , str . lastIndexOf ( TEST_FOLDER + '/' ) + 1 ) ;
96+ return str . substring ( str . lastIndexOf ( '/' ) + 1 , str . length ) ;
97+ }
98+
99+ function getItemsFromPersistance ( ) {
100+ return Backendless . Persistence . of ( FileItem ) . find ( ) . catch ( function ( e ) {
101+ alert ( e . code == 1009 ? 'Please upload a file first' : e . message ) ;
102+ return [ ] ;
103+ } ) ;
104+ }
105+
106+ function uploadFile ( ) {
107+ if ( files . length === 0 ) {
108+ return ;
109+ }
110+
111+ $ ( '#upload-btn' ) . text ( 'Uploading...' ) ;
112+
113+ var requests = files . map ( function ( file ) {
114+ return Backendless . Files . upload ( file , TEST_FOLDER , true ) . then (
115+ function ( result ) {
116+ return createNewItem ( result . fileURL ) ;
117+ }
118+ ) ;
119+ } ) ;
120+
121+ Promise . all ( requests ) . then (
122+ function ( ) {
123+ showInfo ( 'Files successfully uploaded.' ) ;
124+ files = [ ] ;
125+ $ ( '#list' ) . empty ( ) ;
126+ } ,
127+ function ( err ) {
128+ showInfo ( err . message ) ;
129+ }
130+ ) . then ( function ( ) {
131+ $ ( '#upload-btn' ) . text ( 'Upload File' ) ;
132+ } ) ;
133+ }
134+
135+ function deleteSelectedFiles ( ) {
136+ var $selectedElements = $ ( '.selectedThumbnail img' ) ;
137+
138+ if ( $selectedElements . length === 0 ) {
139+ return ;
140+ }
141+
142+ var removeRequests = [ ] ;
143+
144+ $ ( '#delete-btn' ) . text ( 'Deleting...' ) ;
145+
146+ $selectedElements . each ( function ( index , element ) {
147+ removeRequests . push ( Backendless . Files . remove ( element . src ) . then (
148+ function ( ) {
149+ return deleteItem ( element . id ) ;
150+ }
151+ ) ) ;
152+ } ) ;
153+
154+ Promise . all ( removeRequests ) . then (
155+ function ( ) {
156+ showInfo ( 'Objects successfully removed.' ) ;
157+ } ,
158+ function ( err ) {
159+ showInfo ( err . message )
160+ }
161+ ) . then ( function ( ) {
162+ $ ( '#delete-btn' ) . text ( 'Delete Files' ) ;
163+ } ) ;
164+ }
165+
166+ function showInfo ( text ) {
167+ $ ( '#message' ) . text ( text ) ;
168+ var carousel = $ ( '.carousel' ) ;
169+ carousel . carousel ( 2 ) ;
170+ carousel . carousel ( 'pause' ) ;
171+ }
172+
173+ $ ( ) . ready ( init ) ;
174+ } ) ( Backendless , jQuery ) ;
0 commit comments