@@ -12,7 +12,7 @@ import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMedi
12
12
import { guardStream } from '../../util/GuardedStream' ;
13
13
import type { Guarded } from '../../util/GuardedStream' ;
14
14
import { parseContentType } from '../../util/HeaderUtil' ;
15
- import { joinFilePath , isContainerIdentifier } from '../../util/PathUtil' ;
15
+ import { joinFilePath , isContainerIdentifier , isContainerPath } from '../../util/PathUtil' ;
16
16
import { parseQuads , serializeQuads } from '../../util/QuadUtil' ;
17
17
import { addResourceMetadata , updateModifiedDate } from '../../util/ResourceUtil' ;
18
18
import { toLiteral , toNamedTerm } from '../../util/TermUtil' ;
@@ -159,8 +159,14 @@ export class FileDataAccessor implements DataAccessor {
159
159
*/
160
160
private async getFileMetadata ( link : ResourceLink , stats : Stats ) :
161
161
Promise < RepresentationMetadata > {
162
- return ( await this . getBaseMetadata ( link , stats , false ) )
163
- . set ( CONTENT_TYPE_TERM , link . contentType ) ;
162
+ const metadata = await this . getBaseMetadata ( link , stats , false ) ;
163
+ // If the resource is using an unsupported contentType, the original contentType was written to the metadata file.
164
+ // As a result, we should only set the contentType derived from the file path,
165
+ // when no previous metadata entry for contentType is present.
166
+ if ( typeof metadata . contentType === 'undefined' ) {
167
+ metadata . set ( CONTENT_TYPE_TERM , link . contentType ) ;
168
+ }
169
+ return metadata ;
164
170
}
165
171
166
172
/**
@@ -188,7 +194,12 @@ export class FileDataAccessor implements DataAccessor {
188
194
metadata . remove ( RDF . terms . type , LDP . terms . Container ) ;
189
195
metadata . remove ( RDF . terms . type , LDP . terms . BasicContainer ) ;
190
196
metadata . removeAll ( DC . terms . modified ) ;
191
- metadata . removeAll ( CONTENT_TYPE_TERM ) ;
197
+ // When writing metadata for a document, only remove the content-type when dealing with a supported media type.
198
+ // A media type is supported if the FileIdentifierMapper can correctly store it.
199
+ // This allows restoring the appropriate content-type on data read (see getFileMetadata).
200
+ if ( isContainerPath ( link . filePath ) || typeof link . contentType !== 'undefined' ) {
201
+ metadata . removeAll ( CONTENT_TYPE_TERM ) ;
202
+ }
192
203
const quads = metadata . quads ( ) ;
193
204
const metadataLink = await this . resourceMapper . mapUrlToFilePath ( link . identifier , true ) ;
194
205
let wroteMetadata : boolean ;
0 commit comments