1
+ // main/imageToData.js
1
2
const fs = require ( 'fs' ) ;
2
3
const fsExtra = require ( 'fs-extra' ) ;
3
4
const path = require ( 'path' ) ;
@@ -45,43 +46,18 @@ function nibblesToBytes(nibbles) {
45
46
return bytes ;
46
47
}
47
48
48
- function extractFileData ( nibbles ) {
49
- const dataNibbles = [ ] ;
50
- const nameNibbles = [ ] ;
51
- let isReadingName = false ;
52
- for ( let i = 0 ; i < nibbles . length ; i ++ ) {
53
- const nibble = nibbles [ i ] ;
54
- switch ( nibble ) {
55
- case 'A' :
56
- isReadingName = ! isReadingName ;
57
- break ;
58
- case 'D' :
59
- i = nibbles . length ;
60
- break ;
61
- default :
62
- if ( isReadingName ) nameNibbles . push ( nibble ) ;
63
- else dataNibbles . push ( nibble ) ;
64
- }
65
- }
66
- return { dataNibbles, nameNibbles } ;
67
- }
68
-
69
49
async function processWithLimitedWorkers ( filePaths , maxWorkers ) {
70
50
const progressTracker = new Array ( filePaths . length ) . fill ( 0 ) ;
71
51
const processFilesProgress = createIntermediateProgressOutput ( 'Processing Images ->' , filePaths . length ) ;
72
52
let completedFiles = 0 ;
73
-
74
53
const results = [ ] ;
75
54
const activeWorkers = new Set ( ) ;
76
-
77
55
for ( const filePath of filePaths ) {
78
56
while ( activeWorkers . size >= maxWorkers ) {
79
57
await Promise . race ( activeWorkers ) ;
80
58
}
81
-
82
59
const workerPromise = new Promise ( ( resolve , reject ) => {
83
60
const worker = new Worker ( './workers/imageWorker.js' , { workerData : filePath } ) ;
84
-
85
61
worker . on ( 'message' , ( message ) => {
86
62
if ( typeof message === 'number' ) {
87
63
progressTracker [ filePaths . indexOf ( filePath ) ] = message ;
@@ -96,18 +72,15 @@ async function processWithLimitedWorkers(filePaths, maxWorkers) {
96
72
resolve ( message ) ;
97
73
}
98
74
} ) ;
99
-
100
75
worker . on ( 'error' , reject ) ;
101
76
worker . on ( 'exit' , ( code ) => {
102
77
if ( code !== 0 ) reject ( new Error ( `Worker exited with code ${ code } ` ) ) ;
103
78
} ) ;
104
79
} ) ;
105
-
106
80
activeWorkers . add ( workerPromise ) ;
107
81
workerPromise . finally ( ( ) => activeWorkers . delete ( workerPromise ) ) ;
108
82
results . push ( workerPromise ) ;
109
83
}
110
-
111
84
await Promise . all ( results ) ;
112
85
console . log ( "" ) ;
113
86
return results ;
@@ -130,103 +103,62 @@ function playSound(times, delay = 500) {
130
103
131
104
async function main ( ) {
132
105
console . time ( "Processing Time" ) ;
133
-
134
106
try {
135
107
const files = fs . readdirSync ( folderPath ) . sort ( ( a , b ) =>
136
108
a . localeCompare ( b , undefined , { numeric : true } )
137
109
) ;
138
110
const filePaths = files . map ( ( file ) => path . join ( folderPath , file ) ) ;
139
111
const results = await processWithLimitedWorkers ( filePaths , MAX_WORKERS ) ;
140
112
const resolvedResults = await Promise . all ( results ) ;
141
-
142
113
const streamFinished = util . promisify ( require ( 'stream' ) . finished ) ;
143
114
const extractProgress = createIntermediateProgressOutput ( 'Saving file ->' , results . length ) ;
144
- let dataFileStream = null ;
145
115
146
116
for ( let index = 0 ; index < resolvedResults . length ; index ++ ) {
147
117
const tempFilePath = resolvedResults [ index ] ;
148
- let fileContent = fs . readFileSync ( tempFilePath ) ;
118
+ const fileContent = fs . readFileSync ( tempFilePath ) ;
149
119
const identifyFilePath = path . join ( tempFolder , `${ path . parse ( tempFilePath ) . name } .id` ) ;
150
120
121
+ // Nové zpracování identifikačního souboru – očekáváme páry řádků (A a D)
151
122
if ( fs . existsSync ( identifyFilePath ) ) {
152
- const identifyLines = fs . readFileSync ( identifyFilePath , 'utf8' ) . split ( '\n' ) ;
153
- let currentByteOffset = 0 ;
154
-
155
- for ( const line of identifyLines ) {
156
- if ( ! line . trim ( ) ) continue ;
157
-
158
- const regex = / ^ ( \d + ) - ( \d + ) \s ( [ A | D ] ) $ / ;
159
- const match = line . match ( regex ) ;
160
- if ( ! match ) continue ;
161
-
162
- const start = parseInt ( match [ 1 ] , 10 ) ;
163
- const end = parseInt ( match [ 2 ] , 10 ) ;
164
- const identifier = match [ 3 ] ;
165
-
166
- if ( identifier === 'A' ) {
167
- const extractedBytes = fileContent . slice ( start , end ) ;
168
- const extractedName = extractedBytes . toString ( 'utf-8' ) . replace ( / \0 / g, '' ) . trim ( ) ;
169
- const outputFilePath = path . join ( outputFolder , extractedName ) ;
170
-
171
- const outputDir = path . dirname ( outputFilePath ) ;
172
- fsExtra . ensureDirSync ( outputDir ) ;
173
-
174
- if ( dataFileStream ) {
175
- dataFileStream . end ( ) ;
176
- await streamFinished ( dataFileStream ) ;
177
- }
178
-
179
- dataFileStream = fs . createWriteStream ( outputFilePath ) ;
180
- currentByteOffset = 0 ;
181
-
182
- fileContent = Buffer . concat ( [
183
- fileContent . slice ( 0 , start ) ,
184
- fileContent . slice ( end ) ,
185
- ] ) ;
186
- }
187
-
188
- if ( identifier === 'D' ) {
189
- const bytesToWrite = end - currentByteOffset ;
190
- const contentToWrite = fileContent . slice ( 0 , bytesToWrite ) ;
191
-
192
- if ( dataFileStream ) {
193
- dataFileStream . write ( contentToWrite ) ;
194
- currentByteOffset += contentToWrite . length ;
195
- dataFileStream . end ( ) ;
196
- await streamFinished ( dataFileStream ) ;
197
- dataFileStream = null ;
198
- }
199
-
200
- fileContent = fileContent . slice ( bytesToWrite ) ;
123
+ const lines = fs . readFileSync ( identifyFilePath , 'utf8' )
124
+ . split ( '\n' )
125
+ . filter ( line => line . trim ( ) !== "" ) ;
126
+ if ( lines . length % 2 !== 0 ) {
127
+ console . error ( "Invalid identify file format: expected even number of lines." ) ;
128
+ } else {
129
+ for ( let i = 0 ; i < lines . length ; i += 2 ) {
130
+ const aLine = lines [ i ] ;
131
+ const dLine = lines [ i + 1 ] ;
132
+ // Opravený regulární výraz – bez zbytečného "|"
133
+ const matchA = aLine . match ( / ^ ( \d + ) - ( \d + ) \s A $ / ) ;
134
+ const matchD = dLine . match ( / ^ ( \d + ) - ( \d + ) \s D $ / ) ;
135
+ if ( ! matchA || ! matchD ) continue ;
136
+ const aStart = parseInt ( matchA [ 1 ] , 10 ) ;
137
+ const aEnd = parseInt ( matchA [ 2 ] , 10 ) ;
138
+ const dStart = parseInt ( matchD [ 1 ] , 10 ) ;
139
+ const dEnd = parseInt ( matchD [ 2 ] , 10 ) ;
140
+ const nameBuffer = fileContent . slice ( aStart , aEnd ) ;
141
+ const fileName = nameBuffer . toString ( 'utf8' ) . replace ( / \0 / g, '' ) . trim ( ) ;
142
+ const fileData = fileContent . slice ( dStart , dEnd ) ;
143
+ const outputFilePath = path . join ( outputFolder , fileName ) ;
144
+ fsExtra . ensureDirSync ( path . dirname ( outputFilePath ) ) ;
145
+ fs . writeFileSync ( outputFilePath , fileData ) ;
201
146
}
202
147
}
203
148
}
204
-
205
- if ( dataFileStream ) {
206
- dataFileStream . write ( fileContent ) ;
207
- fileContent = null ;
208
- }
209
-
210
149
const progressBar = extractProgress ( index + 1 ) ;
211
150
if ( progressBar ) {
212
151
process . stdout . clearLine ( ) ;
213
152
process . stdout . cursorTo ( 0 ) ;
214
153
process . stdout . write ( progressBar ) ;
215
154
}
216
155
}
217
-
218
- if ( dataFileStream ) {
219
- dataFileStream . end ( ) ;
220
- await streamFinished ( dataFileStream ) ;
221
- }
222
156
} catch ( error ) {
223
157
console . error ( 'Error processing files:' , error ) ;
224
158
playSound ( 2 , 500 ) ;
225
159
}
226
-
227
160
console . log ( "\nAll images processed." ) ;
228
161
console . timeEnd ( "Processing Time" ) ;
229
-
230
162
playSound ( 1 ) ;
231
163
}
232
164
0 commit comments