-
Notifications
You must be signed in to change notification settings - Fork 46
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
Added CSV file support [API, ImportForm] #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const csvParser = (csv) => { | ||
const csvLines = csv.split(/\r\n|\n/); | ||
const headers = csvLines[0].split(','); | ||
const lines = []; | ||
|
||
for (let i = 1; i < csvLines.length; i++) { | ||
const data = csvLines[i].split(','); | ||
|
||
if (data.length == headers.length) { | ||
const jsonObj = {}; | ||
|
||
for (let j = 0; j < headers.length; j++) { | ||
jsonObj[headers[j]] = data[j]; | ||
} | ||
|
||
lines.push(jsonObj); | ||
} | ||
} | ||
|
||
return lines; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,40 @@ | |
const PLUGIN_ID = 'content-export-import'; | ||
|
||
const validator = require('./validations'); | ||
const csvHelpers = require("./csvHelpers"); | ||
|
||
module.exports = { | ||
importContent: async (ctx) => { | ||
const importService = strapi.plugins[PLUGIN_ID].services['contentexportimport']; | ||
|
||
let body = ctx.request.body; | ||
|
||
try { | ||
// Check if request has files | ||
if (ctx.request.files) { | ||
// Get files['source'] | ||
const sourceFile = ctx.request.files["source"].path; | ||
const type = ctx.request.files["source"].type; | ||
|
||
// Read and parse file['source'] | ||
const source = csvHelpers.readFile( | ||
sourceFile, | ||
type === "text/csv" ? csvHelpers.csvParser : JSON.parse | ||
); | ||
|
||
body = { | ||
...body, | ||
source, | ||
}; | ||
|
||
ctx.request.body = body; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try not to re-assign an object? |
||
|
||
console.log(body); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be removed? |
||
} | ||
} catch (err) { | ||
console.error("err", err, ctx.request.files); | ||
} | ||
|
||
const validationResult = validator.validateImportContentRequest( | ||
ctx.request.body); | ||
if (validationResult) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const fs = require('fs'); | ||
|
||
const readFile = (file, parser) => { | ||
try { | ||
const content = fs.readFileSync(file).toString('utf-8'); | ||
console.log('content', content); | ||
return parser && typeof parser === 'function' ? parser(content) : content; | ||
} catch (err) { | ||
throw err; | ||
} | ||
}; | ||
|
||
const csvParser = (csv) => { | ||
const csvLines = csv.split(/\r\n|\n/); | ||
const headers = csvLines[0].split(','); | ||
const lines = []; | ||
|
||
for (let i = 1; i < csvLines.length; i++) { | ||
const data = csvLines[i].split(','); | ||
|
||
if (data.length == headers.length) { | ||
const jsonObj = {}; | ||
|
||
for (let j = 0; j < headers.length; j++) { | ||
jsonObj[headers[j]] = data[j]; | ||
} | ||
|
||
lines.push(jsonObj); | ||
} | ||
} | ||
|
||
return lines; | ||
}; | ||
|
||
module.exports = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether this file could be moved to |
||
readFile, | ||
csvParser | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
csvHelpers
could be afileHelper
or something, otherwise it looks weird to make it parse JSON file as well.