forked from apostrophecms/uploadfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-imagecrunch.js
149 lines (128 loc) · 4.42 KB
/
test-imagecrunch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var uploadfs = require('./uploadfs.js')();
var fs = require('fs');
var _ = require('lodash');
// Test the imagecrunch image backend, written specifically for Macs
var localOptions = { storage: 'local', local: 'imagecrunch', uploadsPath: __dirname + '/test', uploadsUrl: 'http://localhost:3000/test' };
var imageSizes = [
{
name: 'small',
width: 320,
height: 320
},
{
name: 'medium',
width: 640,
height: 640
},
{
name: 'large',
width: 1140,
height: 1140
}
];
var tempPath = __dirname + '/temp';
localOptions.imageSizes = imageSizes;
localOptions.tempPath = tempPath;
localTestStart();
function localTestStart() {
var options = localOptions;
console.log('Initializing uploadfs for the ' + options.storage + ' storage backend with the imagecrunch image backend');
uploadfs.init(options, function(e) {
if (e) {
console.log('uploadfs.init failed:');
console.log(e);
process.exit(1);
}
testCopyImageIn();
});
function testCopyImageIn() {
console.log('testing copyImageIn');
// Note copyImageIn adds an extension for us
uploadfs.copyImageIn('test.jpg', '/images/profiles/me', function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 1936) || (info.height !== 2592)) {
console.log('Width and height missing or not reoriented for web use');
console.log(info);
process.exit(1);
}
if ((info.originalWidth !== 2592) || (info.originalHeight !== 1936)) {
console.log('Original width and height missing or incorrect');
console.log(info);
process.exit(1);
}
var stats = fs.statSync('test/images/profiles/me.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove('/images/profiles/me.jpg', function(e) { });
_.each(imageSizes, function(size) {
var name = info.basePath + '.' + size.name + '.jpg';
var stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) { });
});
testCopyImageInCrop();
});
}
function testCopyImageInCrop() {
console.log('testing copyImageIn with cropping');
// Note copyImageIn adds an extension for us
// Should grab the flowers
uploadfs.copyImageIn('test.jpg', '/images/profiles/me-cropped', { crop: { top: 830, left: 890, width: 500, height: 500 } }, function(e, info) {
if (e) {
console.log('testCopyImageIn failed:');
console.log(e);
process.exit(1);
}
if (info.basePath !== '/images/profiles/me-cropped') {
console.log('info.basePath is incorrect');
process.exit(1);
}
console.log('Testing that returned image dimensions are reoriented');
if ((info.width !== 500) || (info.height !== 500)) {
console.log('Reported size does not match crop');
console.log(info);
process.exit(1);
}
var stats = fs.statSync('test/images/profiles/me-cropped.jpg');
if (!stats.size) {
console.log('Copied image is empty or missing');
process.exit(1);
}
// We already tested remove, just do it to mop up
console.log('Removing files...');
uploadfs.remove('/images/profiles/me-cropped.jpg', function(e) { });
_.each(imageSizes, function(size) {
var name = info.basePath + '.' + size.name + '.jpg';
var stats = fs.statSync('test' + name);
if (!stats.size) {
console.log('Scaled and copied image is empty or missing (2)');
process.exit(1);
}
// We already tested remove, just do it to mop up
uploadfs.remove(info.basePath + '.' + size.name + '.jpg', function(e) { });
});
success();
});
}
function success() {
console.log('All tests passing.');
process.exit(0);
}
}