source: branches/features/purchaseOrders/grails-app/domain/Image.groovy @ 963

Last change on this file since 963 was 880, checked in by gav, 14 years ago

Image unique index fix, by John Yesberg.

File size: 1.1 KB
Line 
1class Image implements Comparable {
2
3    Picture picture
4    Integer size
5    byte[] data
6    String contentType
7    Integer width
8    Integer height
9    Date dateCreated = new Date()
10    Date lastUpdated = new Date()
11
12    static belongsTo = [ Picture ]
13
14    static mapping = {
15        data type: 'binary'
16    }
17
18    static constraints = {
19        data(maxSize: MAX_SIZE)
20        size(unique:'picture')
21    }
22
23    static final Integer MAX_SIZE = 10 * 1024 * 1024
24
25    static final Integer Original = 1
26    static final Integer Large = 2
27    static final Integer Medium = 3
28    static final Integer Small = 4
29
30    static final Integer[] Widths =  [ 0, 0, 500, 250, 100 ]
31    static final Integer[] Heights = [ 0, 0, 500, 250, 100 ]
32
33    static final String[] Names = [ '', 'Original', 'Large', 'Medium', 'Small' ]
34
35    int compareTo(obj) {
36        size.compareTo(obj.size)
37    }
38
39    String filename() {
40        Image.filename(picture.id, size)
41    }
42
43    static String filename(long id, int size) {
44        if (size == Original) {
45            return "${id}-${Names[size]}.jpg"
46        }
47        else {
48            return "${id}-${Names[size]}.png"
49        }
50    }
51}
Note: See TracBrowser for help on using the repository browser.