-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5653 from nextcloud/chore/refactor-attachment-glo…
…bal-store chore: move some attachment logic to global state
- Loading branch information
Showing
6 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* @copyright 2022 Mikhail Sazanov <[email protected]> | ||
* | ||
* @author 2022 Mikhail Sazanov <[email protected]> | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
|
@@ -24,6 +25,7 @@ import axios from '@nextcloud/axios' | |
import { generateOcsUrl, generateRemoteUrl } from '@nextcloud/router' | ||
import { showError, showSuccess } from '@nextcloud/dialogs' | ||
import { translate as t } from '@nextcloud/l10n' | ||
import { parseXML } from 'webdav' | ||
|
||
/** | ||
* Makes a share link for a given file or directory. | ||
|
@@ -85,16 +87,44 @@ const shareFileWith = async function(path, sharedWith, permissions = 17) { | |
|
||
const createFolder = async function(folderName, userId) { | ||
const url = generateRemoteUrl(`dav/files/${userId}/${folderName}`) | ||
await axios({ | ||
method: 'MKCOL', | ||
url, | ||
}).catch(e => { | ||
if (e.response.status !== 405) { | ||
try { | ||
await axios({ | ||
method: 'MKCOL', | ||
url, | ||
}) | ||
} catch (e) { | ||
if (e?.response?.status !== 405) { | ||
showError(t('calendar', 'Error creating a folder {folder}', { | ||
folder: folderName, | ||
})) | ||
// Maybe the actual upload succeeds -> keep going | ||
return folderName | ||
} | ||
}) | ||
|
||
// Folder already exists | ||
if (folderName !== '/') { | ||
folderName = await findFirstOwnedFolder(folderName, userId) | ||
} | ||
} | ||
|
||
return folderName | ||
} | ||
|
||
const findFirstOwnedFolder = async function(path, userId) { | ||
const infoXml = await getFileInfo(path, userId) | ||
const info = await parseXML(infoXml) | ||
const mountType = info?.multistatus?.response[0]?.propstat?.prop?.['mount-type'] | ||
if (mountType !== 'shared') { | ||
return path | ||
} | ||
|
||
const hierarchy = path.split('/') | ||
hierarchy.pop() | ||
if (hierarchy.length === 1) { | ||
return '/' | ||
} | ||
|
||
return findFirstOwnedFolder(hierarchy.join('/'), userId) | ||
} | ||
|
||
const uploadLocalAttachment = async function(folder, files, dav, componentAttachments) { | ||
|
@@ -139,8 +169,8 @@ const uploadLocalAttachment = async function(folder, files, dav, componentAttach | |
} | ||
|
||
// TODO is shared or not @share-types@ | ||
const getFileInfo = async function(path, dav) { | ||
const url = generateRemoteUrl(`dav/files/${dav.userId}/${path}`) | ||
const getFileInfo = async function(path, userId) { | ||
const url = generateRemoteUrl(`dav/files/${userId}/${path}`) | ||
const res = await axios({ | ||
method: 'PROPFIND', | ||
url, | ||
|
@@ -155,6 +185,7 @@ const getFileInfo = async function(path, dav) { | |
<oc:fileid /> | ||
<oc:share-types /> | ||
<nc:has-preview /> | ||
<nc:mount-type /> | ||
</d:prop> | ||
</d:propfind>`, | ||
}).catch(() => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/ | ||
* | ||
* @author Georg Ehrke <[email protected]> | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
|
@@ -27,6 +28,7 @@ import { setConfig as setCalendarJsConfig } from '@nextcloud/calendar-js' | |
import { setConfig } from '../services/settings.js' | ||
import { logInfo } from '../utils/logger.js' | ||
import getTimezoneManager from '../services/timezoneDataProviderService.js' | ||
import * as AttachmentService from '../services/attachmentService.js' | ||
|
||
const state = { | ||
// env | ||
|
@@ -52,6 +54,7 @@ const state = { | |
// user-defined Nextcloud settings | ||
momentLocale: 'en', | ||
attachmentsFolder: '/Calendar', | ||
attachmentsFolderCreated: false, | ||
} | ||
|
||
const mutations = { | ||
|
@@ -143,6 +146,18 @@ const mutations = { | |
*/ | ||
setAttachmentsFolder(state, { attachmentsFolder }) { | ||
state.attachmentsFolder = attachmentsFolder | ||
state.attachmentsFolderCreated = false | ||
}, | ||
|
||
/** | ||
* Update wheter the user's attachments folder has been created | ||
* | ||
* @param {object} state The Vuex state | ||
* @param {object} data The destructuring object | ||
* @param {boolean} data.attachmentsFolderCreated True if the folder has been created | ||
*/ | ||
setAttachmentsFolderCreated(state, { attachmentsFolderCreated }) { | ||
state.attachmentsFolderCreated = attachmentsFolderCreated | ||
}, | ||
|
||
/** | ||
|
@@ -454,6 +469,30 @@ const actions = { | |
commit('setAttachmentsFolder', { attachmentsFolder }) | ||
}, | ||
|
||
/** | ||
* Create the user's attachment folder if it doesn't exist and return its path | ||
* | ||
* @param {object} vuex The Vuex destructuring object | ||
* @param {object} vuex.state The Vuex state | ||
* @param {Function} vuex.commit The Vuex commit Function | ||
* @param {Function} vuex.dispatch The Vuex commit function | ||
* @param {object} vuex.getters The Vuex getters object | ||
* @return {Promise<string>} The path of the user's attachments folder | ||
*/ | ||
async createAttachmentsFolder({ state, commit, dispatch, getters }) { | ||
if (state.attachmentsFolderCreated) { | ||
return state.attachmentsFolder | ||
} | ||
|
||
const userId = getters.getCurrentUserPrincipal.dav.userId | ||
const path = await AttachmentService.createFolder(state.attachmentsFolder, userId) | ||
if (path !== state.attachmentsFolder) { | ||
await dispatch('setAttachmentsFolder', { attachmentsFolder: path }) | ||
} | ||
commit('setAttachmentsFolderCreated', { attachmentsFolderCreated: true }) | ||
return path | ||
}, | ||
|
||
/** | ||
* Initializes the calendar-js configuration | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters