Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/image file class #161

Merged
merged 4 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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