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

Repair photos #897

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions back/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ function onListening() {
*/
require('./src/infrastructure/features/crons/dropbox-synchronizer')
require('./src/infrastructure/features/crons/prevent-sleep')

process.on('unhandledRejection', (error) => console.error(error));
6 changes: 6 additions & 0 deletions back/src/infrastructure/features/api/admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from 'express'
import UpdatePhotos from '../../../use_cases/update-photos'
import UpdateChapter from '../../../use_cases/update-chapter'
import UpdateArticle from '../../../use_cases/update-article'
import UpdateArticles from '../../../use_cases/update-articles'
Expand All @@ -17,4 +18,9 @@ router.patch('/articles/:id/chapters/:position', (req, res) => UpdateChapter.syn
})
.then(() => res.sendStatus(204)))

router.patch('/articles/:id/photos', (req, res) => UpdatePhotos.sync({
dropboxId: req.params.id,
})
.then(() => res.sendStatus(204)))

module.exports = router
54 changes: 54 additions & 0 deletions back/src/use_cases/services/create-article-gallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import flatten from 'lodash/flatten'
import isEmpty from 'lodash/isEmpty'
import photoRepository from '../../domain/repositories/photo-repository'
import DropboxClient from '../../infrastructure/external_services/dropbox-client'

const createArticleGallery = (dropboxFilesPath, dropboxId) => getPhotosOfArticle(dropboxFilesPath, dropboxId)
.then(photos => flatten(photos))
.then(photos => photoRepository.createPhotos(photos))

function getPhotosOfArticle(dropboxFilesPath, dropboxId) {
const paths = filterOnlyGalleryPhotos(dropboxFilesPath)
return createPhotoOfArticle(paths, dropboxId)
}

function filterOnlyGalleryPhotos(paths) {
const photosPaths = paths.filter(path => {
const extension = path.split('.').pop()
return extension === 'jpg' || extension === 'jpeg' || extension === 'png'
})
return photosPaths.filter(path => {
const shortName = path.split('/').pop().substring(0, 3)
return !shortName.match('[iI]mg')
})
}

function createPhotoOfArticle(paths, dropboxId) {
const allImgLinks = paths.reduce((promises, path) => {
const imgLink = serializePhoto(path, dropboxId)
promises.push(imgLink)
return promises
}, [])
return Promise.all(allImgLinks)
}

function serializePhoto(path, dropboxId) {
return DropboxClient.createSharedLink(path)
.then(response => ({
imgLink: _transformToImgLink(response),
dropboxId,
}))
}

function _transformToImgLink(response) {
if (isEmpty(response)) {
console.error('is empty imgLink')
return ''
}
const split = response.url.replace(/....$/, '').split('/s/')
return `${split[0]}/s/raw/${split[1].split('?')[0]}`
}

export default {
createArticleGallery,
}
1 change: 1 addition & 0 deletions back/src/use_cases/synchronize-articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function _shareChapterImage(imgLink) {
.then(_transformToImgLink)
}

// TODO: is different of create-article-gallery.js ???
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take time to work on that

function _transformToImgLink(response) {
return isEmpty(response) ? '' : response.url.replace(/....$/, 'raw=1')
}
Expand Down
42 changes: 2 additions & 40 deletions back/src/use_cases/update-article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,9 @@ import articleRepository from '../domain/repositories/article-repository'
import photoRepository from '../domain/repositories/photo-repository'
import DropboxClient from '../infrastructure/external_services/dropbox-client'
import FileReader from '../infrastructure/external_services/file-reader'
import CreateArticleGallery from './services/create-article-gallery'

async function sync(dropboxId) {
function _createPhotosOfArticlesInDatabase(dropboxFilesPath) {
return getPhotosOfArticle(dropboxFilesPath)
.then(photos => flatten(photos))
.then(photos => photoRepository.createPhotos(photos))
}

function getPhotosOfArticle(dropboxFilesPath) {
const paths = filterOnlyGalleryPhotos(dropboxFilesPath)
return createPhotoOfArticle(paths, dropboxId)
}

function filterOnlyGalleryPhotos(paths) {
const photosPaths = paths.filter(path => {
const extension = path.split('.').pop()
return extension === 'jpg' || extension === 'jpeg' || extension === 'png'
})
return photosPaths.filter(path => {
const shortName = path.split('/').pop().substring(0, 3)
return !shortName.match('[iI]mg')
})
}

function createPhotoOfArticle(paths) {
const allImgLinks = paths.reduce((promises, path) => {
const imgLink = serializePhoto(path)
promises.push(imgLink)
return promises
}, [])
return Promise.all(allImgLinks)
}

function serializePhoto(path) {
return DropboxClient.createSharedLink(path)
.then(response => ({
imgLink: _transformToImgLink(response),
dropboxId,
}))
}

function _createArticlesInDatabase({ addedArticles }) {
return _shareImagesZeros(addedArticles)
.then(articles => articleRepository.create(articles))
Expand Down Expand Up @@ -205,7 +167,7 @@ async function sync(dropboxId) {
}
await _createArticlesInDatabase(report)
await _insertArticlesContentsInDatabase(dropboxFilesPath)
await _createPhotosOfArticlesInDatabase(dropboxFilesPath)
await CreateArticleGallery.createArticleGallery(dropboxFilesPath, dropboxId)
return report
}

Expand Down
13 changes: 13 additions & 0 deletions back/src/use_cases/update-photos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import photoRepository from '../domain/repositories/photo-repository'
import DropboxClient from '../infrastructure/external_services/dropbox-client'
import CreateArticleGallery from './services/create-article-gallery'

async function sync(dropboxId) {
photoRepository.deletePhotosOfArticle(dropboxId)
const dropboxFilesPath = await DropboxClient.getFilesFolderPaths(dropboxId)
return CreateArticleGallery.createArticleGallery(dropboxFilesPath, dropboxId)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to test the returned

}

export default {
sync,
}
15 changes: 13 additions & 2 deletions back/test/infrastructure/external_services/dropbox-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,26 @@ describe('Unit | Infrastructure | dropbox-client', () => {
})

describe('with an error', () => {
it('should return a rejected promise', () => {
beforeEach(() => {
sinon.stub(console, 'error')
})

it('should return a rejected promise', done => {
// given
Dropbox.prototype.sharingCreateSharedLink.rejects(new Error('Expected error'))

// when
const promise = DropboxClient.createSharedLink(path)

// then
return promise.then(link => {
promise.then(link => {
setTimeout(() => {
expect(console.error).to.have.been.calledWith('Erreur lors de la création du lien de : ', '/60/fr.php')
expect(console.error).to.have.been.calledWith('Erreur encore : ', '/60/fr.php')
expect(console.error).to.have.been.callCount(4)
console.error.restore()
done()
}, 1000)
expect(link).to.deep.equal({})
})
})
Expand Down
29 changes: 29 additions & 0 deletions back/test/infrastructure/features/admin.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, request, sinon } from '../../test-helper'
import app from '../../../app'
import UpdatePhotos from '../../../src/use_cases/update-photos'
import UpdateChapter from '../../../src/use_cases/update-chapter'
import UpdateArticle from '../../../src/use_cases/update-article'
import UpdateArticles from '../../../src/use_cases/update-articles'
Expand Down Expand Up @@ -86,4 +87,32 @@ describe('Integration | Routes | admin route', () => {
})
})
})

describe('/admin/articles/:id/photos', () => {
beforeEach(() => {
sinon.stub(UpdatePhotos, 'sync').resolves()
})

afterEach(() => {
UpdatePhotos.sync.restore()
})

it('should call update photos and send 204', done => {
// Given
const stringIdArticle = '59'

// When
request(app)
.patch(`/api/admin/articles/${stringIdArticle}/photos`)
.end((err, response) => {
// Then
expect(UpdatePhotos.sync).to.have.been.calledWith({ dropboxId: stringIdArticle })
expect(response.status).to.deep.equal(204)
if (err) {
done(err)
}
done()
})
})
})
})
Loading