@@ -18,6 +18,7 @@ const fromPairs = require('lodash/fromPairs');
1818const get = require ( 'lodash/get' ) ;
1919const Listr = require ( 'listr' ) ;
2020const { Op } = require ( 'sequelize' ) ;
21+ const path = require ( 'path' ) ;
2122const Promise = require ( 'bluebird' ) ;
2223const { protocol } = require ( '../../config/server/storage' ) ;
2324const storage = require ( '../repository/storage' ) ;
@@ -179,12 +180,13 @@ async function migrateContentElementMeta(element) {
179180}
180181
181182async function getNewMeta ( fileMetas , meta , repositoryId ) {
183+ const repositoryAssetsDir = storage . getPath ( repositoryId ) ;
182184 const newMeta = await Promise . map ( toPairs ( meta ) , async it => {
183185 const [ id , value ] = it ;
184186 if ( ! fileMetas . includes ( id ) ) return it ;
185187 const url = get ( value , 'url' ) ;
186188 if ( ! url ) return it ;
187- const { key, newKey } = getKeysFromUrl ( url , repositoryId ) || { } ;
189+ const { key, newKey } = resolveNewURL ( url , repositoryAssetsDir ) || { } ;
188190 if ( ! key || ! newKey ) return it ;
189191 await storage . copyFile ( key , newKey ) ;
190192 return [ id , {
@@ -199,9 +201,10 @@ async function getNewMeta(fileMetas, meta, repositoryId) {
199201
200202async function imageMigrationHandler ( element ) {
201203 const { repositoryId, data } = element ;
204+ const repositoryAssetsDir = storage . getPath ( repositoryId ) ;
202205 const url = get ( data , 'url' ) ;
203206 if ( ! url ) return data ;
204- const { key, newKey } = getKeysFromUrl ( url , repositoryId ) || { } ;
207+ const { key, newKey } = resolveNewURL ( url , repositoryAssetsDir ) || { } ;
205208 if ( ! key || ! newKey ) return data ;
206209 await storage . copyFile ( key , newKey ) ;
207210 return { ...element . data , url : newKey } ;
@@ -225,14 +228,18 @@ function getMigratedEmbeds(repositoryId, embeds) {
225228
226229async function defaultMigrationHandler ( element ) {
227230 const { repositoryId, data } = element ;
228- const url = get ( element , 'data.assets.url' ) ;
229- if ( ! url ) return data ;
230- const { key, newKey } = getKeysFromUrl ( url , repositoryId ) || { } ;
231- if ( ! key || ! newKey ) return data ;
232- await storage . copyFile ( key , newKey ) ;
231+ const repositoryAssetsDir = storage . getPath ( repositoryId ) ;
232+ const updatedAssets = await Promise
233+ . filter ( toPairs ( data . assets ) , ( [ _ , value ] ) => value . startsWith ( protocol ) )
234+ . map ( async ( [ key , value ] ) => {
235+ const { key : oldKey , newKey } = resolveNewURL ( value , repositoryAssetsDir ) || { } ;
236+ if ( ! oldKey || ! newKey ) return [ key , value ] ;
237+ await storage . copyFile ( oldKey , newKey ) ;
238+ return [ key , `${ protocol } ${ newKey } ` ] ;
239+ } ) ;
233240 return {
234241 ...element . data ,
235- assets : { ...element . data . assets , url : ` ${ protocol } ${ newKey } ` }
242+ assets : { ...element . data . assets , ... fromPairs ( updatedAssets ) }
236243 } ;
237244}
238245
@@ -242,11 +249,11 @@ async function migrateRevision(revision) {
242249 return { state : { ...state , ...payload } } ;
243250}
244251
245- function getKeysFromUrl ( url , repositoryId ) {
246- if ( url . startsWith ( protocol ) ) url = url . substr ( protocol . length ) ;
247- const assetUrl = url . match ( regex ) ;
248- if ( ! assetUrl ) return ;
249- const [ key , sufix ] = assetUrl ;
250- const newKey = ` ${ storage . getPath ( repositoryId ) } / ${ sufix } ` ;
252+ function resolveNewURL ( assetURL , targetDir ) {
253+ if ( assetURL . startsWith ( protocol ) ) assetURL = assetURL . substr ( protocol . length ) ;
254+ const result = assetURL . match ( regex ) ;
255+ if ( ! result ) return ;
256+ const [ key , suffix ] = result ;
257+ const newKey = path . join ( targetDir , suffix ) ;
251258 return { key, newKey } ;
252259}
0 commit comments