Skip to content

Commit

Permalink
Merge pull request #161 from isomerpages/fix/imageFile-class
Browse files Browse the repository at this point in the history
Fix/image file class
  • Loading branch information
alexanderleegs authored Apr 22, 2021
2 parents 84a5fe0 + 9d8b937 commit 77799f5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
14 changes: 6 additions & 8 deletions classes/ImageFile.js → classes/MediaFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { NotFoundError } = require('../errors/NotFoundError')
// Constants
const GITHUB_ORG_NAME = 'isomerpages'

class ImageFile {
class MediaFile {
constructor(accessToken, siteName) {
this.accessToken = accessToken
this.siteName = siteName
Expand All @@ -17,15 +17,13 @@ class ImageFile {
this.fileType = null
}

setFileTypeToImage() {
this.fileType = new ImageType()
setFileTypeToImage(directory) {
this.fileType = new ImageType(directory)
this.baseEndpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/contents/${this.fileType.getFolderName()}`
// Endpoint to retrieve files greater than 1MB
this.baseBlobEndpoint = `https://api.github.com/repos/${GITHUB_ORG_NAME}/${this.siteName}/git/blobs`
}



async list() {
try {
const endpoint = `${this.baseEndpoint}`
Expand Down Expand Up @@ -161,11 +159,11 @@ class ImageFile {
}

class ImageType {
constructor() {
this.folderName = 'images'
constructor(directory) {
this.folderName = directory ? directory : 'images'
}
getFolderName() {
return this.folderName
}
}
module.exports = { ImageFile }
module.exports = { MediaFile }
54 changes: 39 additions & 15 deletions routes/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@ const {

// Import classes
const { File, ImageType } = require('../classes/File.js')
const { ImageFile } = require('../classes/ImageFile.js');
const { update } = require('lodash');
const { MediaFile } = require('../classes/MediaFile.js');

const extractDirectoryAndFileName = (imageName) => {
let imageDirectory, imageFileName

const pathArr = imageName.split('/')
if (pathArr.length === 1) {
imageDirectory = 'images'
imageFileName = imageName
} else if (pathArr.length > 1) {
imageDirectory = `images/${pathArr.slice(0, -1)}`
imageFileName = pathArr[pathArr.length - 1]
}
return {
imageDirectory,
imageFileName,
}
}

// List images
async function listImages (req, res, next) {
Expand All @@ -30,15 +46,14 @@ async function createNewImage (req, res, next) {
const { accessToken } = req

const { siteName } = req.params
const { imageName, content } = req.body
const { imageName, imageDirectory, content } = req.body

// TO-DO:
// Validate imageName and content

const IsomerFile = new File(accessToken, siteName)
const imageType = new ImageType()
IsomerFile.setFileType(imageType)
const { sha } = await IsomerFile.create(imageName, content)
const IsomerImageFile = new MediaFile(accessToken, siteName)
IsomerImageFile.setFileTypeToImage(imageDirectory)
const { sha } = await IsomerImageFile.create(imageName, content)

res.status(200).json({ imageName, content, sha })
}
Expand All @@ -49,9 +64,13 @@ async function readImage (req, res, next) {

const { siteName, imageName } = req.params

const IsomerImageFile = new ImageFile(accessToken, siteName)
IsomerImageFile.setFileTypeToImage()
const { sha, content } = await IsomerImageFile.read(imageName)
// get image directory
const { imageDirectory, imageFileName } = extractDirectoryAndFileName(imageName)

const IsomerImageFile = new MediaFile(accessToken, siteName)
IsomerImageFile.setFileTypeToImage(imageDirectory)

const { sha, content } = await IsomerImageFile.read(imageFileName)

// TO-DO:
// Validate content
Expand Down Expand Up @@ -104,11 +123,16 @@ async function renameImage (req, res, next) {

// Create new file with name ${newImageName}

const IsomerFile = new File(accessToken, siteName)
const imageType = new ImageType()
IsomerFile.setFileType(imageType)
const { sha: newSha } = await IsomerFile.create(newImageName, content)
await IsomerFile.delete(imageName, sha)
const { imageDirectory: oldImageDirectory, imageFileName: oldImageFileName } = extractDirectoryAndFileName(imageName)
const { imageDirectory: newImageDirectory, imageFileName: newImageFileName } = extractDirectoryAndFileName(newImageName)

const newIsomerImageFile = new MediaFile(accessToken, siteName)
newIsomerImageFile.setFileTypeToImage(newImageDirectory)
const { sha: newSha } = await newIsomerImageFile.create(newImageFileName, content)

const oldIsomerImageFile = new MediaFile(accessToken, siteName)
oldIsomerImageFile.setFileTypeToImage(oldImageDirectory)
await oldIsomerImageFile.delete(oldImageFileName, sha)

res.status(200).json({ imageName: newImageName, content, sha: newSha })
}
Expand Down

0 comments on commit 77799f5

Please sign in to comment.