Skip to content

Commit 2bbcb8c

Browse files
committed
Add comments for the “rotate fix”
1 parent 48beb18 commit 2bbcb8c

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import i18n from 'browser/lib/i18n'
1111
const STORAGE_FOLDER_PLACEHOLDER = ':storage'
1212
const DESTINATION_FOLDER = 'attachments'
1313
const PATH_SEPARATORS = escapeStringRegexp(path.posix.sep) + escapeStringRegexp(path.win32.sep)
14-
14+
/**
15+
* @description
16+
* Create a Image element to get the real size of image.
17+
* @param {File} file the File object dropped.
18+
* @returns {Promise<Image>} Image element created
19+
*/
1520
function getImage (file) {
1621
return new Promise((resolve) => {
1722
const reader = new FileReader()
@@ -24,29 +29,54 @@ function getImage (file) {
2429
})
2530
}
2631

32+
/**
33+
* @description
34+
* Get the orientation info from iamges's EXIF data.
35+
* case 1: The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.
36+
* case 2: The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.
37+
* case 3: The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.
38+
* case 4: The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.
39+
* case 5: The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.
40+
* case 6: The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.
41+
* case 7: The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.
42+
* case 8: The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.
43+
* Other: reserved
44+
* ref: http://sylvana.net/jpegcrop/exif_orientation.html
45+
* @param {File} file the File object dropped.
46+
* @returns {Promise<Number>} Orientation info
47+
*/
2748
function getOrientation (file) {
2849
const getData = arrayBuffer => {
2950
const view = new DataView(arrayBuffer)
51+
52+
// Not start with SOI(Start of image) Marker return fail value
3053
if (view.getUint16(0, false) !== 0xFFD8) return -2
3154
const length = view.byteLength
3255
let offset = 2
3356
while (offset < length) {
3457
const marker = view.getUint16(offset, false)
3558
offset += 2
59+
// Loop and seed for APP1 Marker
3660
if (marker === 0xFFE1) {
61+
// return fail value if it isn't EXIF data
3762
if (view.getUint32(offset += 2, false) !== 0x45786966) {
3863
return -1
3964
}
65+
// Read TIFF header,
66+
// First 2bytes defines byte align of TIFF data.
67+
// If it is 0x4949="II", it means "Intel" type byte align.
68+
// If it is 0x4d4d="MM", it means "Motorola" type byte align
4069
const little = view.getUint16(offset += 6, false) === 0x4949
4170
offset += view.getUint32(offset + 4, little)
42-
const tags = view.getUint16(offset, little)
71+
const tags = view.getUint16(offset, little) // Get TAG number
4372
offset += 2
4473
for (let i = 0; i < tags; i++) {
74+
// Loop to find Orientation TAG and return the value
4575
if (view.getUint16(offset + (i * 12), little) === 0x0112) {
4676
return view.getUint16(offset + (i * 12) + 8, little)
4777
}
4878
}
49-
} else if ((marker & 0xFF00) !== 0xFF00) {
79+
} else if ((marker & 0xFF00) !== 0xFF00) { // If not start with 0xFF, not a Marker
5080
break
5181
} else {
5282
offset += view.getUint16(offset, false)
@@ -60,7 +90,13 @@ function getOrientation (file) {
6090
reader.readAsArrayBuffer(file.slice(0, 64 * 1024))
6191
})
6292
}
63-
93+
/**
94+
* @description
95+
* Rotate image file to correct direction.
96+
* Create a canvas and draw the image with correct direction, then export to base64 format.
97+
* @param {*} file the File object dropped.
98+
* @return {String} Base64 encoded image.
99+
*/
64100
function fixRotate (file) {
65101
return Promise.all([getImage(file), getOrientation(file)])
66102
.then(([img, orientation]) => {

0 commit comments

Comments
 (0)