forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes TryGhost#4608, TryGhost#4609 - image handler loads in any image files & figures out where they'll get stored - image importer has a preprocessor which replaces image paths in pertinent spots of post, tag and user models - image importer stores images, keeping the path where it makes sense - basic test for the preprocessor
- Loading branch information
Showing
26 changed files
with
461 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var _ = require('lodash'), | ||
Promise = require('bluebird'), | ||
path = require('path'), | ||
config = require('../../../config'), | ||
storage = require('../../../storage'), | ||
|
||
ImageHandler; | ||
|
||
ImageHandler = { | ||
type: 'images', | ||
extensions: config.uploads.extensions, | ||
types: config.uploads.contentTypes, | ||
|
||
loadFile: function (files, startDir) { | ||
var store = storage.getStorage(), | ||
startDirRegex = startDir ? new RegExp('^' + startDir + '/') : new RegExp(''), | ||
imageFolderRegexes = _.map(config.paths.imagesRelPath.split('/'), function (dir) { | ||
return new RegExp('^' + dir + '/'); | ||
}); | ||
|
||
// normalize the directory structure | ||
files = _.map(files, function (file) { | ||
var noStartDir = file.name.replace(startDirRegex, ''), | ||
noGhostDirs = noStartDir; | ||
|
||
_.each(imageFolderRegexes, function (regex) { | ||
noGhostDirs = noGhostDirs.replace(regex, ''); | ||
}); | ||
|
||
file.originalPath = noStartDir; | ||
file.name = noGhostDirs; | ||
file.targetDir = path.join(config.paths.imagesPath, path.dirname(noGhostDirs)); | ||
return file; | ||
}); | ||
|
||
return Promise.map(files, function (image) { | ||
return store.getUniqueFileName(store, image, image.targetDir).then(function (targetFilename) { | ||
image.newPath = (config.paths.subdir + '/' + | ||
config.paths.imagesRelPath + '/' + path.relative(config.paths.imagesPath, targetFilename)) | ||
.replace(new RegExp('\\' + path.sep, 'g'), '/'); | ||
return image; | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = ImageHandler; |
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,73 @@ | ||
var _ = require('lodash'), | ||
Promise = require('bluebird'), | ||
storage = require('../../../storage'), | ||
replaceImage, | ||
ImageImporter, | ||
preProcessPosts, | ||
preProcessTags, | ||
preProcessUsers; | ||
|
||
replaceImage = function (markdown, image) { | ||
// Normalizes to include a trailing slash if there was one | ||
var regex = new RegExp('(/)?' + image.originalPath, 'gm'); | ||
|
||
return markdown.replace(regex, image.newPath); | ||
}; | ||
|
||
preProcessPosts = function (data, image) { | ||
_.each(data.posts, function (post) { | ||
post.markdown = replaceImage(post.markdown, image); | ||
if (post.html) { | ||
post.html = replaceImage(post.html, image); | ||
} | ||
if (post.image) { | ||
post.image = replaceImage(post.image, image); | ||
} | ||
}); | ||
}; | ||
|
||
preProcessTags = function (data, image) { | ||
_.each(data.tags, function (tag) { | ||
if (tag.image) { | ||
tag.image = replaceImage(tag.image, image); | ||
} | ||
}); | ||
}; | ||
|
||
preProcessUsers = function (data, image) { | ||
_.each(data.users, function (user) { | ||
if (user.cover) { | ||
user.cover = replaceImage(user.cover, image); | ||
} | ||
if (user.image) { | ||
user.image = replaceImage(user.image, image); | ||
} | ||
}); | ||
}; | ||
|
||
ImageImporter = { | ||
type: 'images', | ||
preProcess: function (importData) { | ||
if (importData.images && importData.data) { | ||
_.each(importData.images, function (image) { | ||
preProcessPosts(importData.data.data, image); | ||
preProcessTags(importData.data.data, image); | ||
preProcessUsers(importData.data.data, image); | ||
}); | ||
} | ||
|
||
importData.preProcessedByImage = true; | ||
return importData; | ||
}, | ||
doImport: function (imageData) { | ||
var store = storage.getStorage(); | ||
|
||
return Promise.map(imageData, function (image) { | ||
return store.save(image, image.targetDir).then(function (result) { | ||
return {originalPath: image.originalPath, newPath: image.newPath, stored: result}; | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = ImageImporter; |
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.