public parseImageBucket()

in source/image-handler/image-request.ts [151:185]


  public parseImageBucket(event: ImageHandlerEvent, requestType: RequestTypes): string {
    if (requestType === RequestTypes.DEFAULT) {
      // Decode the image request
      const request = this.decodeRequest(event);

      if (request.bucket !== undefined) {
        // Check the provided bucket against the allowed list
        const sourceBuckets = this.getAllowedSourceBuckets();

        if (sourceBuckets.includes(request.bucket) || request.bucket.match(new RegExp('^' + sourceBuckets[0] + '$'))) {
          return request.bucket;
        } else {
          throw new ImageHandlerError(
            StatusCodes.FORBIDDEN,
            'ImageBucket::CannotAccessBucket',
            'The bucket you specified could not be accessed. Please check that the bucket is specified in your SOURCE_BUCKETS.'
          );
        }
      } else {
        // Try to use the default image source bucket env var
        const sourceBuckets = this.getAllowedSourceBuckets();
        return sourceBuckets[0];
      }
    } else if (requestType === RequestTypes.THUMBOR || requestType === RequestTypes.CUSTOM) {
      // Use the default image source bucket env var
      const sourceBuckets = this.getAllowedSourceBuckets();
      return sourceBuckets[0];
    } else {
      throw new ImageHandlerError(
        StatusCodes.NOT_FOUND,
        'ImageBucket::CannotFindBucket',
        'The bucket you specified could not be found. Please check the spelling of the bucket name in your request.'
      );
    }
  }