-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.js
More file actions
135 lines (110 loc) · 3.15 KB
/
Copy pathFile.js
File metadata and controls
135 lines (110 loc) · 3.15 KB
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
"use strict";
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
module.exports = File;
function File( _version){
this.version = _version;
}
File.prototype.fromDisk = function( fileIn, orgName, cb){
if ( !fileExists( fileIn ) )
throw "File can't be readed";
this.path = fileIn;
if (orgName)
this.name = orgName;
else
this.name = path.basename( fileIn );
// El largo del nombre viaja en un solo byte, y se escribe en UTF-8:
// el límite es 255 bytes, no 256 caracteres.
if (Buffer.byteLength(this.name, "utf8") > 255)
throw new Error("Filename must be at most 255 bytes (UTF-8)");
fs.readFile( fileIn, ( err, _buffer) => {
zlib.gzip( _buffer, {level: 9}, (err, cbuffer) => {
this.fileBuffer = cbuffer;
cb(err, this.toBytes());
});
} )
}
File.prototype.toBytes = function(){
let i;
let bufferName = Buffer.from( this.name, "utf8");
let total = 2 + bufferName.length + this.fileBuffer.length; // 1 version, 1 fnamesize, filename, filedata
let buffer = Buffer.alloc(total);
buffer[0] = this.version;
buffer[1] = bufferName.length;
let idx = 2;
for(i=0; i<bufferName.length;i++){
buffer[idx++] = bufferName[i];
}
for(i=0; i<this.fileBuffer.length;i++){
buffer[idx++] = this.fileBuffer[i];
}
return buffer;
}
File.prototype.toDisk = function(buffer, fileOut, cb){
let frame;
try{
frame = File.parseFrame( buffer );
}catch(ex){
return cb( ex.message );
}
this.version = 1;
this.name = frame.name;
File.writeGunzip( frame.payload, fileOut, (err) => {
if (err) return cb(err);
cb( undefined, this.name ); // se devuelve el nombre al terminar
} );
}
// --- Framing (solo formato legacy) -----------------------------------------
//
// Las imágenes nuevas NO llevan frame: el nombre viaja en el prólogo (container.js) y
// la región codificada tiene solo el gzip. Esto queda para leer las imágenes viejas.
File.parseFrame = function( bytes ){
const version = bytes[0];
if (version !== 1)
throw new Error("Not a valid MICodec file. Take care to don't use applications like hangout, whatsapp to transfer it, they modify de image.");
const nameLen = bytes[1];
return {
name: bytes.toString( "utf8", 2, 2 + nameLen ),
payload: bytes.subarray( 2 + nameLen ),
};
}
// --- gzip, sin framing -----------------------------------------------------
//
// El camino nuevo solo necesita comprimir/descomprimir: el nombre ya está en el prólogo.
File.gzipFile = function( fileIn, cb ){
fs.readFile( fileIn, (err, buffer) => {
if (err) return cb(err);
zlib.gzip( buffer, { level: 9 }, cb );
} );
}
File.writeGunzip = function( payload, fileOut, cb ){
zlib.gunzip( payload, (err, out) => {
if (err) return cb(err);
try{
fs.writeFileSync( fileOut, out );
cb();
}catch(ex){
cb(ex);
}
} );
}
// Funcniones auxiliares
function fileExists(fileIn){
try {
fs.accessSync( fileIn, fs.constants.F_OK);
return true;
} catch (e) {
return false;
}
}
/*
File.prototype.toArrayBuffer = function(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
}
*/