<commit_before>package com.cellarhq.dbimport.simpledb

import com.amazonaws.services.simpledb.model.Item
import com.cellarhq.generated.Tables
import com.cellarhq.generated.tables.records.CellarRecord
import com.cellarhq.generated.tables.records.DrinkRecord
import com.cellarhq.generated.tables.records.OrganizationRecord
import com.cellarhq.generated.tables.records.PhotoRecord
import org.jooq.DSLContext
import org.jooq.UpdatableRecord
import org.jooq.exception.DataAccessException

import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.sql.Timestamp
import java.time.LocalDateTime

class SimpleDBPhotoImporter {

    static String ATTR_PHOTO_URL = 'photoUrl'
    static String ATTR_PHOTO_FULL_SIZE_URL = 'photoFullSizeUrl'

    AmazonHelper amazonHelper = new AmazonHelper()
    DSLContext create

    SimpleDBPhotoImporter(DSLContext create) {
        this.create = create
    }

    void importPhoto(Item item, UpdatableRecord ownerRecord) {
        if (!hasPhoto(item)) {
            return
        }

        PhotoRecord photoRecord = create.newRecord(Tables.PHOTO).with { PhotoRecord self ->
            version = 0
            createdDate = Timestamp.valueOf(LocalDateTime.now())
            modifiedDate = createdDate
            originalUrl = getImageUrl(amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_FULL_SIZE_URL).trim())
            thumbUrl = getImageUrl(amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_URL).trim())
            largeUrl = thumbUrl
            description = 'imported from simpledb'

            return self
        }

        BufferedImage originalImage = getBufferedImage(photoRecord.originalUrl)
        if (!getBufferedImage(photoRecord.originalUrl)) {
            println('Could not read original image; aborting import of photo')
            return
        }

        BufferedImage thumbImage = getBufferedImage(photoRecord.thumbUrl)
        if (!thumbImage) {
            println('Could not read thumb image; replacing with original')
            photoRecord.thumbUrl = photoRecord.originalUrl
            photoRecord.largeUrl = photoRecord.originalUrl
            thumbImage = originalImage
        }

        photoRecord.with {
            thumbWidth = largeWidth = thumbImage.width
            thumbHeight = largeHeight = thumbImage.height
        }

        try {
            photoRecord.store()

            switch (ownerRecord.class) {
                case CellarRecord:
                    ((CellarRecord) ownerRecord).photoId = photoRecord.id
                    break

                case DrinkRecord:
                    ((DrinkRecord) ownerRecord).photoId = photoRecord.id
                    break

                case OrganizationRecord:
                    ((OrganizationRecord) ownerRecord).photoId = photoRecord.id
                    break
            }
        } catch (DataAccessException e) {
            println "Error inserting photo because ${e.message} with ${photoRecord}"
        }
    }

    private boolean hasPhoto(Item item) {
        return amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_FULL_SIZE_URL).trim()
    }

    private String getImageUrl(String path) {
        return "http://storage-www.cellarhq.com.s3-website-us-east-1.amazonaws.com/${path}"
    }

    private BufferedImage getBufferedImage(String url) {
        return ImageIO.read(new URL(url))
    }
}<commit_msg>Photo import no longer throws an IOException<commit_after>package com.cellarhq.dbimport.simpledb

import com.amazonaws.services.simpledb.model.Item
import com.cellarhq.generated.Tables
import com.cellarhq.generated.tables.records.CellarRecord
import com.cellarhq.generated.tables.records.DrinkRecord
import com.cellarhq.generated.tables.records.OrganizationRecord
import com.cellarhq.generated.tables.records.PhotoRecord
import org.jooq.DSLContext
import org.jooq.UpdatableRecord
import org.jooq.exception.DataAccessException

import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.sql.Timestamp
import java.time.LocalDateTime

class SimpleDBPhotoImporter {

    static String ATTR_PHOTO_URL = 'photoUrl'
    static String ATTR_PHOTO_FULL_SIZE_URL = 'photoFullSizeUrl'

    AmazonHelper amazonHelper = new AmazonHelper()
    DSLContext create

    SimpleDBPhotoImporter(DSLContext create) {
        this.create = create
    }

    void importPhoto(Item item, UpdatableRecord ownerRecord) {
        if (!hasPhoto(item)) {
            return
        }

        PhotoRecord photoRecord = create.newRecord(Tables.PHOTO).with { PhotoRecord self ->
            version = 0
            createdDate = Timestamp.valueOf(LocalDateTime.now())
            modifiedDate = createdDate
            originalUrl = getImageUrl(amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_FULL_SIZE_URL).trim())
            thumbUrl = getImageUrl(amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_URL).trim())
            largeUrl = thumbUrl
            description = 'imported from simpledb'

            return self
        }

        BufferedImage originalImage = getBufferedImage(photoRecord.originalUrl)
        if (!originalImage) {
            println('Could not read original image; aborting import of photo')
            return
        }

        BufferedImage thumbImage = getBufferedImage(photoRecord.thumbUrl)
        if (!thumbImage) {
            println('Could not read thumb image; replacing with original')
            photoRecord.thumbUrl = photoRecord.originalUrl
            photoRecord.largeUrl = photoRecord.originalUrl
            thumbImage = originalImage
        }

        photoRecord.with {
            thumbWidth = largeWidth = thumbImage.width
            thumbHeight = largeHeight = thumbImage.height
        }

        try {
            photoRecord.store()

            switch (ownerRecord.class) {
                case CellarRecord:
                    ((CellarRecord) ownerRecord).photoId = photoRecord.id
                    break

                case DrinkRecord:
                    ((DrinkRecord) ownerRecord).photoId = photoRecord.id
                    break

                case OrganizationRecord:
                    ((OrganizationRecord) ownerRecord).photoId = photoRecord.id
                    break
            }
        } catch (DataAccessException e) {
            println "Error inserting photo because ${e.message} with ${photoRecord}"
        }
    }

    private boolean hasPhoto(Item item) {
        return amazonHelper.getAttribute(item.attributes, ATTR_PHOTO_FULL_SIZE_URL).trim()
    }

    private String getImageUrl(String path) {
        return "http://storage-www.cellarhq.com.s3-website-us-east-1.amazonaws.com/${path}"
    }

    private BufferedImage getBufferedImage(String url) {
        try {
            return ImageIO.read(new URL(url))
        } catch (IOException e) {
            return null
        }
    }
}
{'fraction_non_alphanumeric': 0.07663225243565508, 'fraction_numerical': 0.0008724734622655228, 'mean_word_length': 2.4458917835671343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3636861', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1791, 'n_words': 362}