forked from remotestorage/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpictures.js
122 lines (97 loc) · 3.06 KB
/
pictures.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
// Pictures
//
// This module stores collections of pictures, called "albums".
// Each folder at the root of the module is treated as an album,
// unless it starts with a dollar sign ($)
//
RemoteStorage.defineModule('pictures', function(privateClient, publicClient) {
function isDir(path) {
return path.substr(-1) == '/';
}
function bindAll(object) {
for(var key in this) {
if(typeof(object[key]) == 'function') {
object[key] = object[key].bind(object);
}
}
}
// Albums only work, when the user is connected and online.
var Album = function(name, client) {
this.name = name;
this.client = client;
this.prefix = encodeURIComponent(this.name) + '/';
// Sync all picture names, but not the pictures themselves.
// this.client.use(this.prefix, true);
// Bind all the things
bindAll(this);
};
Album.prototype = {
// Store image with given MIME type under the given name.
// The given `data` is expected to be an `ArrayBuffer`.
//
// Returns a promise, which will be fulfilled with the
// absolute URL of the newly uploaded picture.
// See `getPictureURL` for details.
store: function(mimeType, fileName, data) {
return this.client.storeFile(
mimeType,
this._path(fileName),
data,
false // << skip the cache
).then(function() {
return this.getPictureURL(fileName);
}.bind(this));
},
remove: function(fileName) {
return this.client.remove(this._path(fileName));
},
// Get a list of all pictures in this album.
list: function() {
return this.client.getListing(this.prefix).
then(function(listing) {
return listing.map(decodeURIComponent);
});
},
// Get the absolute URL for the picture with the given `fileName`.
// Useful for displaying a public picture using the `src` attribute
// of an `<img>` element.
getPictureURL: function(fileName) {
return this.client.getItemURL(this._path(fileName));
},
close: function() {
this.client.cache(this.prefix, false);
},
_path: function(fileName) {
return this.prefix + encodeURIComponent(fileName);
}
};
function filterAlbumListing(listing) {
var albums = [];
listing.forEach(function(item) {
if(isDir(item) && item[0] !== '$') {
albums.push(item.replace(/\/$/, '')); // strip trailing slash.
}
});
return albums;
}
var pictures = {
getUuid: privateClient.uuid,
// Open album with given `name`. This will sync the list of images
// and make them accessible via the returned `Album` object.
openPublicAlbum: function(name) {
return new Album(name, publicClient);
},
listPublicAlbums: function() {
return publicClient.getListing('').then(filterAlbumListing);
},
openPrivateAlbum: function(name) {
return new Album(name, privateClient);
},
listPrivateAlbums: function() {
return privateClient.getListing('').then(filterAlbumListing);
}
};
return {
exports: pictures
};
});