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: use selected format instead of file extension for Content-Type header #52

Merged
merged 1 commit into from
Jul 17, 2019
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
13 changes: 2 additions & 11 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2018-11-26T10:01:30.830Z\n"
"PO-Revision-Date: 2018-11-26T10:01:30.830Z\n"
"POT-Creation-Date: 2019-07-12T08:54:25.308Z\n"
"PO-Revision-Date: 2019-07-12T08:54:25.308Z\n"

msgid "user is not logged in"
msgstr ""
Expand Down Expand Up @@ -137,9 +137,6 @@ msgstr ""
msgid "Identifier"
msgstr ""

msgid "Import Mode"
msgstr ""

msgid "Report Mode"
msgstr ""

Expand Down Expand Up @@ -248,12 +245,6 @@ msgstr ""
msgid "Check (safe, recommended)"
msgstr ""

msgid "Commit"
msgstr ""

msgid "Validate"
msgstr ""

msgid "AUTO"
msgstr ""

Expand Down
30 changes: 9 additions & 21 deletions src/helpers/mime.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
export function getMimeType(filename) {
if (!filename) {
return null
}

const isJSON = filename.endsWith('json') || filename.includes('.json')
const isXML = filename.endsWith('xml') || filename.includes('.xml')
const isCSV = filename.endsWith('csv') || filename.includes('.csv')
const isGML = filename.endsWith('gml') || filename.includes('.gml')

if (isJSON) {
return 'application/json'
} else if (isXML) {
return 'application/xml'
} else if (isCSV) {
return 'application/csv'
} else if (isGML) {
return 'application/xml'
}

return null
const mapping = {
json: 'application/json',
xml: 'application/xml',
csv: 'application/csv',
gml: 'application/xml',
adx: 'application/adx+xml',
pdf: 'application/pdf',
}

export const getMimeType = format => mapping[format] || null
5 changes: 3 additions & 2 deletions src/helpers/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { getMimeType } from './mime'
import { eventEmitter } from 'services'
import { emitLogOnFirstResponse, fetchLog } from 'pages/import/helpers'

export function getUploadXHR(url, upload, type, onResponse, onError) {
// eslint-disable-next-line max-params
export function getUploadXHR(url, upload, type, onResponse, onError, format) {
const xhr = new XMLHttpRequest()
const contentType = getMimeType(upload.name.toLowerCase())
const contentType = getMimeType(format)

xhr.withCredentials = true
xhr.open('POST', url, true)
Expand Down
13 changes: 9 additions & 4 deletions src/pages/import/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ export class DataImport extends FormBase {

onSubmit = () => {
try {
const { upload, format } = this.getFormState()
const formData = new FormData()
formData.set('upload', upload)
const { upload, format, firstRowIsHeader } = this.getFormState()
const formattedFormat = format.substr(1)
const append = [`format=${formattedFormat}`, 'async=true']

if (format === '.csv') {
append.push(`firstRowIsHeader=${firstRowIsHeader}`)
}

const params = getParamsFromFormState(
this.getFormState(),
Expand All @@ -91,7 +95,8 @@ export class DataImport extends FormBase {
upload,
'DATAVALUE_IMPORT',
this.clearProcessing,
this.assertOnError
this.assertOnError,
formattedFormat
)

xhr.send(upload)
Expand Down
6 changes: 4 additions & 2 deletions src/pages/import/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ export class EventImport extends FormBase {
onSubmit = async () => {
try {
const { upload, format } = this.getFormState()
const formattedFormat = format.slice(1)

const params = getParamsFromFormState(
this.getFormState(),
['dryRun', 'eventIdScheme', 'orgUnitIdScheme'],
[
'async=true',
'skipFirst=true',
`payloadFormat=${format.slice(1)}`,
`payloadFormat=${formattedFormat}`,
]
)
this.setProcessing()
Expand All @@ -67,7 +68,8 @@ export class EventImport extends FormBase {
upload,
'EVENT_IMPORT',
this.clearProcessing,
this.assertOnError
this.assertOnError,
formattedFormat
)
xhr.send(upload)
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/import/GML.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export class GMLImport extends FormBase {
upload,
'GML_IMPORT',
this.clearProcessing,
this.assertOnError
this.assertOnError,
'gml'
)
xhr.send(upload)
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/import/MetaData.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export class MetaDataImport extends FormBase {
upload,
'METADATA_IMPORT',
this.clearProcessing,
this.assertOnError
this.assertOnError,
format.substr(1)
)
xhr.send(upload)
} catch (e) {
Expand Down