'From Squeak3.2gamma of 22 February 2002 [latest update: #4849] on 3 May 2002 at 7:01:55 pm'! "Change Set: resFix Date: 3 May 2002 Author: Michael Rueger Recreates the image resource file if it can not be found in any of the original locations. This may happen when a loaded project gets out of sync with it's environment, e.g. by moving the image to a different location."! !ResourceCollector methodsFor: 'resource writing' stamp: 'mir 4/30/2002 16:42'! writeResourceForm: aForm fromLocator: aLocator "The given form has been externalized before. If it was reasonably compressed, use the bits of the original data - this allows us to recycle GIF, JPEG, PNG etc. data without using the internal compression (which is in most cases inferior). If necessary the data will be retrieved from its URL location. This retrieval is done only if the resouce comes from either * the local disk (in which case the file has never been published) * the browser cache (in which case we don't cache the resource locally) In any other case we will *not* attempt to retrieve it, because doing so can cause the system to connect to the network which is probably not what we want. It should be a rare case anyways; could only happen if one clears the squeak cache selectively." | fName fStream url data | "Try to be smart about the name of the file" fName _ (aLocator urlString includes: $:) ifTrue: [ url _ aLocator urlString asUrl. url path last] ifFalse: [aLocator urlString]. fName isEmptyOrNil ifFalse:[fName _ fName asFileName]. (fName isEmptyOrNil or:[localDirectory isAFileNamed: fName]) ifTrue:[ "bad luck -- duplicate name" fName _ localDirectory nextNameFor:'resource' extension: (FileDirectory extensionFor: aLocator urlString)]. "Let's see if we have cached it locally" ResourceManager lookupCachedResource: self baseUrl , aLocator urlString ifPresentDo:[:stream | data _ stream upToEnd]. "Check if the cache entry is without qualifying baseUrl. Workaround for older versions." data ifNil:[ ResourceManager lookupCachedResource: aLocator urlString ifPresentDo:[:stream | data _ stream upToEnd]]. data ifNil:[ "We don't have it cached locally. Retrieve it from its original location." ((url notNil and: [url hasRemoteContents]) and:[HTTPClient isRunningInBrowser not]) ifTrue:[^nil]. "see note above" (Url schemeNameForString: aLocator urlString) ifNil: [^nil]. data _ HTTPLoader default retrieveContentsFor: aLocator urlString. data ifNil:[^nil]. data _ data content. ]. data size > aForm bits byteSize ifTrue:[^nil]. fStream _ localDirectory newFileNamed: fName. fStream nextPutAll: data. fStream close. ^{fName. data size}! ! !ResourceCollector methodsFor: 'resource writing' stamp: 'mir 5/3/2002 19:01'! writeResourceForm: aForm locator: aLocator "Store the given form on a file. Return an array with the name and the size of the file" | fName fStream fullSize result writerClass | aLocator ifNotNil:[ result _ self writeResourceForm: aForm fromLocator: aLocator. result ifNotNil:[^result] "else fall through" ]. fName _ localDirectory nextNameFor:'resource' extension:'form'. fStream _ localDirectory newFileNamed: fName. fStream binary. "aForm storeResourceOn: fStream." writerClass _ ((Smalltalk includesKey: #JPEGReaderWriter2) and: [(Smalltalk at: #JPEGReaderWriter2) new isPluginPresent]) ifTrue: [(Smalltalk at: #JPEGReaderWriter2)] ifFalse: [GIFReadWriter]. writerClass putForm: aForm onStream: fStream. fStream open. fullSize _ fStream size. fStream close. "Compress contents here" " fStream position: 0. fStream compressFile. localDirectory deleteFileNamed: fName. localDirectory rename: fName, FileDirectory dot, 'gz' toBe: fName. fStream _ localDirectory readOnlyFileNamed: fName. fullSize _ fStream size. fStream close. " ^{fName. fullSize}! !