1
1
import * as fs from "fs-extra" ;
2
2
import FileType , { FileTypeResult } from "file-type" ;
3
- import axios from "axios" ;
4
- import * as Path from "path" ;
5
3
import { makeImagePersistencePlan } from "./MakeImagePersistencePlan" ;
6
- import { warning , logDebug , verbose , info } from "./log" ;
4
+ import { logDebug , verbose } from "./log" ;
7
5
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types" ;
8
6
import {
9
7
IDocuNotionContext ,
10
8
IDocuNotionContextPageInfo ,
11
9
IPlugin ,
12
10
} from "./plugins/pluginTypes" ;
11
+ import { writeAsset } from "./assets" ;
13
12
14
13
// We handle several things here:
15
14
// 1) copy images locally instead of leaving them in Notion
@@ -150,19 +149,26 @@ async function processImageBlock(
150
149
async function readPrimaryImage ( imageSet : ImageSet ) {
151
150
// In Mar 2024, we started having a problem getting a particular gif from imgur using
152
151
// node-fetch. Switching to axios resolved it. I don't know why.
153
- const response = await axios . get ( imageSet . primaryUrl , {
154
- responseType : "arraybuffer" ,
155
- } ) ;
156
- imageSet . primaryBuffer = Buffer . from ( response . data , "utf-8" ) ;
152
+ // Then, in Apr 2025, we started getting 429 responses from imgur through axios,
153
+ // so we switched to node's built-in fetch (different than the node-fetch package).
154
+ // Just a guess, but probably imgur keeps locking down what it suspects as code running
155
+ // to scrape images.
156
+ // Apparently, imgur is getting to be more and more of a liability,
157
+ // so we should probably stop using it.
158
+ const response = await fetch ( imageSet . primaryUrl ) ;
159
+ const arrayBuffer = await response . arrayBuffer ( ) ;
160
+ imageSet . primaryBuffer = Buffer . from ( arrayBuffer ) ;
157
161
imageSet . fileType = await FileType . fromBuffer ( imageSet . primaryBuffer ) ;
158
162
}
159
163
160
164
async function saveImage ( imageSet : ImageSet ) : Promise < void > {
161
- writeImageIfNew ( imageSet . primaryFileOutputPath ! , imageSet . primaryBuffer ! ) ;
165
+ const path = imageSet . primaryFileOutputPath ! ;
166
+ imageWasSeen ( path ) ;
167
+ writeAsset ( path , imageSet . primaryBuffer ! ) ;
162
168
163
169
for ( const localizedImage of imageSet . localizedUrls ) {
164
170
let buffer = imageSet . primaryBuffer ! ;
165
- // if we have a urls for the localized screenshot, download it
171
+ // if we have a url for the localized screenshot, download it
166
172
if ( localizedImage ?. url . length > 0 ) {
167
173
verbose ( `Retrieving ${ localizedImage . iso632Code } version...` ) ;
168
174
const response = await fetch ( localizedImage . url ) ;
@@ -180,30 +186,15 @@ async function saveImage(imageSet: ImageSet): Promise<void> {
180
186
imageSet . pageInfo ! . relativeFilePathToFolderContainingPage
181
187
} `;
182
188
183
- writeImageIfNew (
184
- ( directory + "/" + imageSet . outputFileName ! ) . replaceAll ( "//" , "/" ) ,
185
- buffer
189
+ const newPath = ( directory + "/" + imageSet . outputFileName ! ) . replaceAll (
190
+ "//" ,
191
+ "/"
186
192
) ;
193
+ imageWasSeen ( newPath ) ;
194
+ writeAsset ( newPath , buffer ) ;
187
195
}
188
196
}
189
197
190
- function writeImageIfNew ( path : string , buffer : Buffer ) {
191
- imageWasSeen ( path ) ;
192
-
193
- // Note: it's tempting to not spend time writing this out if we already have
194
- // it from a previous run. But we don't really know it's the same. A) it
195
- // could just have the same name, B) it could have been previously
196
- // unlocalized and thus filled with a copy of the primary language image
197
- // while and now is localized.
198
- if ( fs . pathExistsSync ( path ) ) {
199
- verbose ( "Replacing image " + path ) ;
200
- } else {
201
- verbose ( "Adding image " + path ) ;
202
- fs . mkdirsSync ( Path . dirname ( path ) ) ;
203
- }
204
- fs . createWriteStream ( path ) . write ( buffer ) ; // async but we're not waiting
205
- }
206
-
207
198
export function parseImageBlock ( image : any ) : ImageSet {
208
199
if ( ! locales ) throw Error ( "Did you call initImageHandling()?" ) ;
209
200
const imageSet : ImageSet = {
0 commit comments