import StorageErrorFactory from "../errors/StorageErrorFactory" ; import { ModifiedAccessConditions } from "../generated/artifacts/models" ; import Context from "../generated/Context" ; import { BlobModel , ContainerModel } from "../persistence/IBlobMetadataStore" ; import ConditionalHeadersAdapter from "./ConditionalHeadersAdapter" ; import ConditionResourceAdapter from "./ConditionResourceAdapter" ; import { IConditionalHeaders } from "./IConditionalHeaders" ; import { IConditionalHeadersValidator } from "./IConditionalHeadersValidator" ; import IConditionResource from "./IConditionResource" ; export function validateReadConditions ( context , conditionalHeaders ? , model ? ) { new ReadConditionalHeadersValidator ( ) . validate ( context , new ConditionalHeadersAdapter ( context , conditionalHeaders ) , new ConditionResourceAdapter ( model ) ) ; } export default class ReadConditionalHeadersValidator implements IConditionalHeadersValidator { link param context param conditionalHeaders param resource public validate ( context , conditionalHeaders , resource ) { if ( ! resource . exist ) { if ( conditionalHeaders . ifMatch && conditionalHeaders . ifMatch . length > 0 ) { throw StorageErrorFactory . getConditionNotMet ( context . contextId ! ) ; } if ( conditionalHeaders . ifNoneMatch && conditionalHeaders . ifNoneMatch . length > 0 && conditionalHeaders . ifNoneMatch [ 0 ] === "*" ) { throw StorageErrorFactory . getUnsatisfiableCondition ( context . contextId ! ) ; } } else { const ifMatchPass = conditionalHeaders . ifMatch ? conditionalHeaders . ifMatch . includes ( resource . etag ) || conditionalHeaders . ifMatch [ 0 ] === "*" : undefined ; const ifUnModifiedSincePass = conditionalHeaders . ifUnmodifiedSince ? resource . lastModified <= conditionalHeaders . ifUnmodifiedSince : undefined ; if ( conditionalHeaders . ifNoneMatch && conditionalHeaders . ifNoneMatch . length > 0 && conditionalHeaders . ifNoneMatch [ 0 ] === "*" ) { throw StorageErrorFactory . getUnsatisfiableCondition ( context . contextId ! ) ; } const ifNoneMatchPass = conditionalHeaders . ifNoneMatch ? ! conditionalHeaders . ifNoneMatch . includes ( resource . etag ) : undefined ; const isModifiedSincePass = conditionalHeaders . ifModifiedSince ? conditionalHeaders . ifModifiedSince < resource . lastModified : undefined ; if ( ifMatchPass === false ) { throw StorageErrorFactory . getConditionNotMet ( context . contextId ! ) ; } if ( ifUnModifiedSincePass === false ) { throw StorageErrorFactory . getConditionNotMet ( context . contextId ! ) ; } if ( ifNoneMatchPass === false && isModifiedSincePass !== true ) { throw StorageErrorFactory . getNotModified ( context . contextId ! ) ; } if ( isModifiedSincePass === false && ifNoneMatchPass !== true ) { throw StorageErrorFactory . getNotModified ( context . contextId ! ) ; } } } }