Completed
Pull Request — master (#753)
by Juan Pablo
62:49
created

FileList.inList   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 = {
0 ignored issues
show
Bug introduced by
Redefinition of 'FileList'.
Loading history...
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 {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
40
			return null;
41
		}
42
	},
43
44
	inList: function(file) {
45
		return this.findFile(file);
46
	},
47
48
	/**
49
	 * Create an empty file inside the current album.
50
	 *
51
	 * @param {string} name name of the file
52
	 *
53
	 * @return {Promise} promise that will be resolved after the
54
	 * file was created
55
	 *
56
	 */
57
	createFile: function(name) {
58
		var self = this;
59
		var deferred = $.Deferred();
60
		var promise = deferred.promise();
61
62
		OCA.Files.isFileNameValid(name);
63
64
		var targetPath = this.getCurrentDirectory() + '/' + name;
65
66
		//Check if file already exists
67
		if(Gallery.imageMap[targetPath]) {
68
			OC.Notification.showTemporary(
69
				t('files', 'Could not create file "{file}" because it already exists', {file: name})
70
			);
71
			deferred.reject();
72
			return promise;
73
		}
74
75
		Gallery.filesClient.putFileContents(
76
			targetPath,
77
			'',
78
			{
79
				contentType: 'text/plain',
80
				overwrite: true
81
			}
82
			)
83
			.done(function() {
84
				// TODO: error handling / conflicts
85
				Gallery.filesClient.getFileInfo(
86
					targetPath, {
87
						properties: self.findFile(targetPath)
88
					}
89
					)
90
					.then(function(status, data) {
91
						deferred.resolve(status, data);
92
					})
93
					.fail(function(status) {
94
						OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name}));
95
						deferred.reject(status);
96
					});
97
			})
98
			.fail(function(status) {
99
				if (status === 412) {
100
					OC.Notification.showTemporary(
101
						t('files', 'Could not create file "{file}" because it already exists', {file: name})
102
					);
103
				} else {
104
					OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name}));
105
				}
106
				deferred.reject(status);
107
			});
108
109
		return promise;
110
	},
111
112
113
	/**
114
	 * Retrieves the current album
115
	 *
116
	 * @returns {string}
117
	 */
118
	getCurrentDirectory: function () {
119
		"use strict";
120
121
		// In Files, dirs start with a /
122
		return '/' + Gallery.currentAlbum;
123
	},
124
125
	getUploadUrl: function(fileName, dir) {
126
		if (_.isUndefined(dir)) {
127
			dir = this.getCurrentDirectory();
128
		}
129
130
		var pathSections = dir.split('/');
131
		if (!_.isUndefined(fileName)) {
132
			pathSections.push(fileName);
133
		}
134
		var encodedPath = '';
135
		_.each(pathSections, function(section) {
136
			if (section !== '') {
137
				encodedPath += '/' + encodeURIComponent(section);
138
			}
139
		});
140
		return OC.linkToRemoteBase('webdav') + encodedPath;
141
	}
142
};
143
144
/**
145
 * OCA.Files methods needed for file uploading
146
 *
147
 * This hack makes it possible to use the Files scripts as is, without having to import and
148
 * maintain them in Gallery
149
 *
150
 * @type {{isFileNameValid: Files.isFileNameValid, generatePreviewUrl: Files.generatePreviewUrl}}
151
 */
152
var Files = {
153
	App: {fileList: {}},
154
155
	isFileNameValid: function (name) {
156
		"use strict";
157
		var trimmedName = name.trim();
158
		if (trimmedName === '.' || trimmedName === '..') {
159
			throw t('files', '"{name}" is an invalid file name.', {name: name});
160
		} else if (trimmedName.length === 0) {
161
			throw t('files', 'File name cannot be empty.');
162
		}
163
		return true;
164
165
	},
166
167
	/**
168
	 * Generates a preview for the conflict dialogue
169
	 *
170
	 * Since Gallery uses the fileId and Files uses the path, we have to use the preview endpoint
171
	 * of Files
172
	 */
173
	generatePreviewUrl: function (urlSpec) {
174
		"use strict";
175
		var previewUrl;
176
		var path = urlSpec.file;
177
178
		// In Files, root files start with //
179
		if (path.indexOf('//') === 0) {
180
			path = path.substring(2);
181
		} else {
182
			// Directories start with /
183
			path = path.substring(1);
184
		}
185
186
		if (Gallery.imageMap[path]) {
187
			var fileId = Gallery.imageMap[path].fileId;
188
			var thumbnail = Thumbnails.map[fileId];
189
			previewUrl = thumbnail.image.src;
190
		} else {
191
			var previewDimension = 96;
192
			urlSpec.x = Math.ceil(previewDimension * window.devicePixelRatio);
193
			urlSpec.y = Math.ceil(previewDimension * window.devicePixelRatio);
194
			urlSpec.forceIcon = 0;
195
			previewUrl = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
196
		}
197
198
		return previewUrl;
199
	}
200
};
201
202
OCA.Files = Files;
203
OCA.Files.App.fileList = FileList;
204