forked from nextcloud/photos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.js
More file actions
257 lines (228 loc) · 7.39 KB
/
files.js
File metadata and controls
257 lines (228 loc) · 7.39 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
import moment from '@nextcloud/moment'
import { showError } from '@nextcloud/dialogs'
import logger from '../services/logger.js'
import client, { prefixPath } from '../services/DavClient.js'
import Semaphore from '../utils/semaphoreWithPriority.js'
const state = {
files: {},
nomediaPaths: [],
}
const mutations = {
/**
* Append or update given files
*
* @param {object} state the store mutations
* @param {Array} newFiles the store mutations
*/
updateFiles(state, newFiles) {
const files = {}
newFiles.forEach(file => {
// Ignore the file if the path is excluded
if (state.nomediaPaths.some(nomediaPath => file.filename.startsWith(nomediaPath)
|| file.filename.startsWith(prefixPath + nomediaPath))) {
return
}
if (file.fileid >= 0) {
if (file.fileMetadataSize?.length > 1) {
file.fileMetadataSize = file.fileMetadataSize === '' ? '{}' : file.fileMetadataSize
file.fileMetadataSizeParsed = JSON.parse(file.fileMetadataSize?.replace(/"/g, '"') ?? '{}')
file.fileMetadataSizeParsed.width = file.fileMetadataSizeParsed?.width ?? 256
file.fileMetadataSizeParsed.height = file.fileMetadataSizeParsed?.height ?? 256
} else {
file.fileMetadataSizeParsed = { width: 256, height: 256 }
}
}
// Make the fileId a string once and for all.
file.fileid = file.fileid.toString()
// Precalculate dates as it is expensive.
file.timestamp = moment(file.lastmod).unix() // For sorting
file.month = moment(file.lastmod).format('YYYYMM') // For grouping by month
file.day = moment(file.lastmod).format('MMDD') // For On this day
// Schedule the file to add
files[file.fileid] = file
})
state.files = {
...state.files,
...files,
}
},
/**
* Set a folder subfolders
*
* @param {object} state the store mutations
* @param {object} data destructuring object
* @param {number} data.fileid current folder id
* @param {Array} data.folders list of folders
*/
setSubFolders(state, { fileid, folders }) {
if (state.files[fileid]) {
const subfolders = folders
.map(folder => folder.fileid)
// some invalid folders have an id of -1 (ext storage)
.filter(id => id >= 0)
Vue.set(state.files[fileid], 'folders', subfolders)
}
},
/**
* Set list of all .nomedia/.noimage files
*
* @param {object} state the store mutations
* @param {Array} paths list of files
*/
setNomediaPaths(state, paths) {
state.nomediaPaths = paths
},
/**
* Delete a file
*
* @param {object} state the store mutations
* @param {number} fileId - The id of the file
*/
deleteFile(state, fileId) {
Vue.delete(state.files, fileId)
},
/**
* Favorite a list of files
*
* @param {object} state the store mutations
* @param {object} params -
* @param {number} params.fileId - The id of the file
* @param {0|1} params.favoriteState - The ew state of the favorite property
*/
favoriteFile(state, { fileId, favoriteState }) {
Vue.set(state.files[fileId], 'favorite', favoriteState)
},
}
const getters = {
files: state => state.files,
nomediaPaths: state => state.nomediaPaths,
}
const actions = {
/**
* Update files, folders and their respective subfolders
*
* @param {object} context the store mutations
* @param {object} data destructuring object
* @param {object} data.folder current folder fileinfo
* @param {Array} data.files list of files
* @param {Array} data.folders list of folders within current folder
*/
updateFiles(context, { folder, files = [], folders = [] } = {}) {
// we want all the FileInfo! Folders included!
context.commit('updateFiles', [folder, ...files, ...folders])
context.commit('setSubFolders', { fileid: folder.fileid, folders })
},
/**
* Append or update given files
*
* @param {object} context the store mutations
* @param {Array} files list of files
*/
appendFiles(context, files = []) {
context.commit('updateFiles', files)
},
/**
* Set list of all .nomedia/.noimage files
*
* @param {object} context the store mutations
* @param {Array} paths list of files
*/
setNomediaPaths(context, paths) {
logger.debug('Ignored paths', { paths })
context.commit('setNomediaPaths', paths)
},
/**
* Delete a list of files
*
* @param {object} context the store mutations
* @param {number[]} fileIds - The ids of the files
*/
deleteFiles(context, fileIds) {
const semaphore = new Semaphore(5)
const files = fileIds
.map(fileId => state.files[fileId])
.reduce((files, file) => ({ ...files, [file.fileid]: file }), {})
fileIds.forEach(fileId => context.commit('deleteFile', fileId))
const promises = fileIds
.map(async (fileId) => {
const file = files[fileId]
const symbol = await semaphore.acquire()
try {
await client.deleteFile(file.filename)
} catch (error) {
logger.error(t('photos', 'Failed to delete {fileId}.', { fileId }), { error })
showError(t('photos', 'Failed to delete {fileName}.', { fileName: file.basename }))
console.error(error)
context.dispatch('appendFiles', [file])
} finally {
semaphore.release(symbol)
}
})
return Promise.all(promises)
},
/**
* Favorite a list of files
*
* @param {object} context the store mutations
* @param {object} params -
* @param {number[]} params.fileIds - The ids of the files
* @param {0|1} params.favoriteState - The favorite state to set
*/
toggleFavoriteForFiles(context, { fileIds, favoriteState }) {
const semaphore = new Semaphore(5)
const promises = fileIds
.map(async (fileId) => {
const file = context.state.files[fileId]
const symbole = await semaphore.acquire()
try {
context.commit('favoriteFile', { fileId, favoriteState })
await client.customRequest(
file.filename,
{
method: 'PROPPATCH',
data: `<?xml version="1.0"?>
<d:propertyupdate xmlns:d="DAV:"
xmlns:oc="http://owncloud.org/ns"
xmlns:nc="http://nextcloud.org/ns"
xmlns:ocs="http://open-collaboration-services.org/ns">
<d:set>
<d:prop>
<oc:favorite>${favoriteState}</oc:favorite>
</d:prop>
</d:set>
</d:propertyupdate>`,
}
)
} catch (error) {
context.commit('favoriteFile', { fileId, favoriteState: favoriteState === 0 ? 1 : 0 })
logger.error(t('photos', 'Failed to set favorite state for {fileId}.', { fileId: file.fileid }), { error })
showError(t('photos', 'Failed to set favorite state for {fileName}.', { fileName: file.basename }))
}
return semaphore.release(symbole)
})
return Promise.all(promises)
},
}
export default { state, mutations, getters, actions }