|
1
|
|
|
/* global Gallery, Thumbnails */ |
|
2
|
|
|
/** |
|
3
|
|
|
* OCA.FileList methods needed for file uploading |
|
4
|
|
|
* |
|
5
|
|
|
* This hack makes it possible to use the Files scripts as is, without having to import and |
|
6
|
|
|
* maintain them in Gallery |
|
7
|
|
|
* |
|
8
|
|
|
* Empty methods are for the "new" button, if we want to implement that one day |
|
9
|
|
|
* |
|
10
|
|
|
* @type {{inList: FileList.inList, lastAction: FileList.lastAction, getUniqueName: |
|
11
|
|
|
* FileList.getUniqueName, getCurrentDirectory: FileList.getCurrentDirectory, add: |
|
12
|
|
|
* FileList.add, checkName: FileList.checkName}} |
|
13
|
|
|
*/ |
|
14
|
|
|
var FileList = { |
|
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Makes sure the filename does not exist |
|
17
|
|
|
* |
|
18
|
|
|
* Gives an early chance to the user to abort the action, before uploading everything to the |
|
19
|
|
|
* server. |
|
20
|
|
|
* Albums are not supported as we don't have a full list of images contained in a sub-album |
|
21
|
|
|
* |
|
22
|
|
|
* @param fileName |
|
23
|
|
|
* @returns {*} |
|
24
|
|
|
*/ |
|
25
|
|
|
findFile: function (fileName) { |
|
26
|
|
|
"use strict"; |
|
27
|
|
|
var path = Gallery.currentAlbum + '/' + fileName; |
|
28
|
|
|
var galleryImage = Gallery.imageMap[path]; |
|
29
|
|
|
if (galleryImage) { |
|
30
|
|
|
var fileInfo = { |
|
31
|
|
|
name: fileName, |
|
32
|
|
|
directory: Gallery.currentAlbum, |
|
33
|
|
|
path: path, |
|
34
|
|
|
etag: galleryImage.etag, |
|
35
|
|
|
mtime: galleryImage.mTime * 1000, // Javascript gives the Epoch time in milliseconds |
|
36
|
|
|
size: galleryImage.size |
|
37
|
|
|
}; |
|
38
|
|
|
return fileInfo; |
|
39
|
|
|
} else { |
|
|
|
|
|
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
}, |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create an empty file inside the current album. |
|
46
|
|
|
* |
|
47
|
|
|
* @param {string} name name of the file |
|
48
|
|
|
* |
|
49
|
|
|
* @return {Promise} promise that will be resolved after the |
|
50
|
|
|
* file was created |
|
51
|
|
|
* |
|
52
|
|
|
*/ |
|
53
|
|
|
createFile: function(name) { |
|
54
|
|
|
var self = this; |
|
55
|
|
|
var deferred = $.Deferred(); |
|
56
|
|
|
var promise = deferred.promise(); |
|
57
|
|
|
|
|
58
|
|
|
OCA.Files.isFileNameValid(name); |
|
59
|
|
|
|
|
60
|
|
|
var targetPath = this.getCurrentDirectory() + '/' + name; |
|
61
|
|
|
|
|
62
|
|
|
//Check if file already exists |
|
63
|
|
|
if(Gallery.imageMap[targetPath]) { |
|
64
|
|
|
OC.Notification.showTemporary( |
|
65
|
|
|
t('files', 'Could not create file "{file}" because it already exists', {file: name}) |
|
66
|
|
|
); |
|
67
|
|
|
deferred.reject(); |
|
68
|
|
|
return promise; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
Gallery.filesClient.putFileContents( |
|
72
|
|
|
targetPath, |
|
73
|
|
|
'', |
|
74
|
|
|
{ |
|
75
|
|
|
contentType: 'text/plain', |
|
76
|
|
|
overwrite: true |
|
77
|
|
|
} |
|
78
|
|
|
) |
|
79
|
|
|
.done(function() { |
|
80
|
|
|
// TODO: error handling / conflicts |
|
81
|
|
|
Gallery.filesClient.getFileInfo( |
|
82
|
|
|
targetPath, { |
|
83
|
|
|
properties: self.findFile(targetPath) |
|
84
|
|
|
} |
|
85
|
|
|
) |
|
86
|
|
|
.then(function(status, data) { |
|
87
|
|
|
deferred.resolve(status, data); |
|
88
|
|
|
}) |
|
89
|
|
|
.fail(function(status) { |
|
90
|
|
|
OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name})); |
|
91
|
|
|
deferred.reject(status); |
|
92
|
|
|
}); |
|
93
|
|
|
}) |
|
94
|
|
|
.fail(function(status) { |
|
95
|
|
|
if (status === 412) { |
|
96
|
|
|
OC.Notification.showTemporary( |
|
97
|
|
|
t('files', 'Could not create file "{file}" because it already exists', {file: name}) |
|
98
|
|
|
); |
|
99
|
|
|
} else { |
|
100
|
|
|
OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name})); |
|
101
|
|
|
} |
|
102
|
|
|
deferred.reject(status); |
|
103
|
|
|
}); |
|
104
|
|
|
|
|
105
|
|
|
return promise; |
|
106
|
|
|
}, |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Retrieves the current album |
|
111
|
|
|
* |
|
112
|
|
|
* @returns {string} |
|
113
|
|
|
*/ |
|
114
|
|
|
getCurrentDirectory: function () { |
|
115
|
|
|
"use strict"; |
|
116
|
|
|
|
|
117
|
|
|
// In Files, dirs start with a / |
|
118
|
|
|
return '/' + Gallery.currentAlbum; |
|
119
|
|
|
}, |
|
120
|
|
|
|
|
121
|
|
|
getUploadUrl: function(fileName, dir) { |
|
122
|
|
|
if (_.isUndefined(dir)) { |
|
123
|
|
|
dir = this.getCurrentDirectory(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
var pathSections = dir.split('/'); |
|
127
|
|
|
if (!_.isUndefined(fileName)) { |
|
128
|
|
|
pathSections.push(fileName); |
|
129
|
|
|
} |
|
130
|
|
|
var encodedPath = ''; |
|
131
|
|
|
_.each(pathSections, function(section) { |
|
132
|
|
|
if (section !== '') { |
|
133
|
|
|
encodedPath += '/' + encodeURIComponent(section); |
|
134
|
|
|
} |
|
135
|
|
|
}); |
|
136
|
|
|
return OC.linkToRemoteBase('webdav') + encodedPath; |
|
137
|
|
|
} |
|
138
|
|
|
}; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* OCA.Files methods needed for file uploading |
|
142
|
|
|
* |
|
143
|
|
|
* This hack makes it possible to use the Files scripts as is, without having to import and |
|
144
|
|
|
* maintain them in Gallery |
|
145
|
|
|
* |
|
146
|
|
|
* @type {{isFileNameValid: Files.isFileNameValid, generatePreviewUrl: Files.generatePreviewUrl}} |
|
147
|
|
|
*/ |
|
148
|
|
|
var Files = { |
|
149
|
|
|
App: {fileList: {}}, |
|
150
|
|
|
|
|
151
|
|
|
isFileNameValid: function (name) { |
|
152
|
|
|
"use strict"; |
|
153
|
|
|
var trimmedName = name.trim(); |
|
154
|
|
|
if (trimmedName === '.' || trimmedName === '..') { |
|
155
|
|
|
throw t('files', '"{name}" is an invalid file name.', {name: name}); |
|
156
|
|
|
} else if (trimmedName.length === 0) { |
|
157
|
|
|
throw t('files', 'File name cannot be empty.'); |
|
158
|
|
|
} |
|
159
|
|
|
return true; |
|
160
|
|
|
|
|
161
|
|
|
}, |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Generates a preview for the conflict dialogue |
|
165
|
|
|
* |
|
166
|
|
|
* Since Gallery uses the fileId and Files uses the path, we have to use the preview endpoint |
|
167
|
|
|
* of Files |
|
168
|
|
|
*/ |
|
169
|
|
|
generatePreviewUrl: function (urlSpec) { |
|
170
|
|
|
"use strict"; |
|
171
|
|
|
var previewUrl; |
|
172
|
|
|
var path = urlSpec.file; |
|
173
|
|
|
|
|
174
|
|
|
// In Files, root files start with // |
|
175
|
|
|
if (path.indexOf('//') === 0) { |
|
176
|
|
|
path = path.substring(2); |
|
177
|
|
|
} else { |
|
178
|
|
|
// Directories start with / |
|
179
|
|
|
path = path.substring(1); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (Gallery.imageMap[path]) { |
|
183
|
|
|
var fileId = Gallery.imageMap[path].fileId; |
|
184
|
|
|
var thumbnail = Thumbnails.map[fileId]; |
|
185
|
|
|
previewUrl = thumbnail.image.src; |
|
186
|
|
|
} else { |
|
187
|
|
|
var previewDimension = 96; |
|
188
|
|
|
urlSpec.x = Math.ceil(previewDimension * window.devicePixelRatio); |
|
189
|
|
|
urlSpec.y = Math.ceil(previewDimension * window.devicePixelRatio); |
|
190
|
|
|
urlSpec.forceIcon = 0; |
|
191
|
|
|
previewUrl = OC.generateUrl('/core/preview.png?') + $.param(urlSpec); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return previewUrl; |
|
195
|
|
|
} |
|
196
|
|
|
}; |
|
197
|
|
|
|
|
198
|
|
|
OCA.Files = Files; |
|
199
|
|
|
OCA.Files.App.fileList = FileList; |
|
200
|
|
|
|