-
Notifications
You must be signed in to change notification settings - Fork 2
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 #174 from isomerpages/develop
Merge to prod 12 May
- Loading branch information
Showing
11 changed files
with
303 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const _ = require('lodash') | ||
|
||
const { File, ImageType, DocumentType } = require('./File.js') | ||
const { getTree, sendTree } = require('../utils/utils.js') | ||
|
||
class MediaSubfolder { | ||
constructor(accessToken, siteName, fileType) { | ||
this.accessToken = accessToken | ||
this.siteName = siteName | ||
switch (fileType) { | ||
case 'images': | ||
this.fileType = new ImageType() | ||
this.mediaFolderName = fileType | ||
break | ||
case 'documents': | ||
this.fileType = new DocumentType() | ||
this.mediaFolderName = 'files' | ||
break | ||
default: | ||
throw new Error("Invalid media type!") | ||
} | ||
} | ||
|
||
async create(subfolderPath) { | ||
try { | ||
const IsomerFile = new File(this.accessToken, this.siteName) | ||
IsomerFile.setFileType(this.fileType) | ||
await IsomerFile.create(`${subfolderPath}/.keep`, '') | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
async delete(subfolderPath, currentCommitSha, treeSha) { | ||
try { | ||
const commitMessage = `Delete ${this.mediaFolderName} subfolder ${subfolderPath}` | ||
const gitTree = await getTree(this.siteName, this.accessToken, treeSha, true) | ||
const directoryName = `${this.mediaFolderName}/${subfolderPath}` | ||
const newGitTree = [] | ||
gitTree.forEach(item => { | ||
if (item.path.includes(directoryName)) { | ||
return | ||
} else if (item.type === 'tree' && item.path.includes(this.mediaFolderName)) { | ||
// We don't include any trees in the media folder - we reconstruct them by adding all their individual files instead | ||
return | ||
} else { | ||
newGitTree.push(item) | ||
} | ||
}) | ||
await sendTree(newGitTree, currentCommitSha, this.siteName, this.accessToken, commitMessage) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
|
||
async rename(oldSubfolderPath, newSubfolderPath, currentCommitSha, treeSha) { | ||
try { | ||
const commitMessage = `Rename ${this.mediaFolderName} subfolder from ${oldSubfolderPath} to ${newSubfolderPath}` | ||
|
||
const gitTree = await getTree(this.siteName, this.accessToken, treeSha, true); | ||
const oldDirectoryName = `${this.mediaFolderName}/${oldSubfolderPath}` | ||
const newDirectoryName = `${this.mediaFolderName}/${newSubfolderPath}` | ||
const newGitTree = [] | ||
gitTree.forEach(item => { | ||
if (item.path === oldDirectoryName) { | ||
newGitTree.push({ | ||
...item, | ||
path: newDirectoryName | ||
}) | ||
} else if (item.path.includes(oldDirectoryName)) { | ||
// We don't want to include these because they use the old path, they are included with the renamed tree | ||
return | ||
} else if (item.type === 'tree' && item.path.includes(this.mediaFolderName)) { | ||
// We don't include any other trees - we reconstruct them by adding all their individual files instead | ||
return | ||
} else { | ||
newGitTree.push(item) | ||
} | ||
}) | ||
await sendTree(newGitTree, currentCommitSha, this.siteName, this.accessToken, commitMessage); | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
module.exports = { MediaSubfolder } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.