diff -r 72b966a7c3d3 -r 192fd647de07 Filename.st --- a/Filename.st Fri Nov 19 14:27:43 1999 +0100 +++ b/Filename.st Fri Nov 19 15:17:40 1999 +0100 @@ -2670,6 +2670,101 @@ "Modified: / 21.9.1998 / 15:33:07 / cg" ! +mimeTypeOfContents + "this tries to guess the mime type of contents of + the file. Returns nil, if the file is unreadable, not a plain file + or the contents is unknown. + This is done using some heuristics, and may need some improvement" + + |type buffer s n suffix idx idx2 baseNm| + + self isDirectory ifTrue:[ + ^ 'nil' + ]. + (type := self type) == #characterSpecial ifTrue:[ + ^ nil + ]. + type == #blockSpecial ifTrue:[ + ^ nil + ]. + type == #socket ifTrue:[ + ^ nil + ]. + + self isReadable ifFalse:[^ nil]. + self fileSize == 0 ifTrue:[^ nil]. + + suffix := self suffix asLowercase. + baseNm := self withoutSuffix baseName asLowercase. + + "/ read some data from the file ... + buffer := String new:2024. + s := self readStream. + s isNil ifTrue:[^ nil]. + + n := s nextBytes:buffer size into:buffer. + s close. + + (idx := buffer findString:'MIMEType:') ~~ 0 ifTrue:[ +self halt. + idx := idx + 'MIMEType:' size. + idx2 := buffer indexOf:Character cr startingAt:idx. + idx > idx ifTrue:[ +self halt. + ^ buffer copyFrom:idx to:idx. + ]. + ]. + + (buffer findString:'subclass:') ~~ 0 ifTrue:[ + ^ 'application/x-smalltalk-source' + ]. + (buffer findString:'methodsFor:') ~~ 0 ifTrue:[ + ^ 'application/x-smalltalk-source' + ]. + + (buffer findString:'