66import android .provider .OpenableColumns ;
77import android .webkit .MimeTypeMap ;
88
9- import com .hzy .libmagic .MagicApi ;
10-
119import java .io .File ;
1210import java .io .FileInputStream ;
1311import java .io .FileNotFoundException ;
@@ -21,30 +19,6 @@ public MetadataLoader(Context context) {
2119 super (context , LoaderType .METADATA );
2220 }
2321
24- private boolean initMagicFromAssets () {
25- InputStream inputStream = null ;
26- try {
27- inputStream = context .getAssets ().open ("magic.mgc" );
28- int length = inputStream .available ();
29- byte [] buffer = new byte [length ];
30- if (inputStream .read (buffer ) > 0 ) {
31- return MagicApi .loadFromBytes (buffer , MagicApi .MAGIC_MIME_TYPE | MagicApi .MAGIC_COMPRESS_TRANSP ) == 0 ;
32- }
33- } catch (Throwable e ) {
34- crashManager .log (e );
35- } finally {
36- if (inputStream != null ) {
37- try {
38- inputStream .close ();
39- } catch (IOException e ) {
40- crashManager .log (e );
41- }
42- }
43- }
44-
45- return false ;
46- }
47-
4822 @ Override
4923 public boolean isSupported (Options options ) {
5024 return true ;
@@ -113,22 +87,20 @@ public void loadSync(Options options) {
11387 String [] fileSplit = options .filename .split ("\\ ." );
11488 String extension = fileSplit .length > 0 ? fileSplit [fileSplit .length - 1 ] : "N/A" ;
11589
116- String type = null ;
90+ String mimetype = null ;
11791 try {
118- if (initMagicFromAssets ()) {
119- type = MagicApi .magicFile (cachedFile .getAbsolutePath ());
120- }
92+ mimetype = CoreWrapper .mimetype (cachedFile .getAbsolutePath ());
12193 } catch (Throwable e ) {
12294 crashManager .log (e );
12395 }
12496
125- if (type == null ) {
126- type = context .getContentResolver ().getType (uri );
97+ if (mimetype == null ) {
98+ mimetype = context .getContentResolver ().getType (uri );
12799 }
128100
129- if (type == null ) {
101+ if (mimetype == null ) {
130102 try {
131- type = URLConnection .guessContentTypeFromName (filename );
103+ mimetype = URLConnection .guessContentTypeFromName (filename );
132104 } catch (Exception e ) {
133105 // Samsung S7 Edge crashes with java.lang.StringIndexOutOfBoundsException
134106 crashManager .log (e );
@@ -138,27 +110,27 @@ public void loadSync(Options options) {
138110 if (type == null ) {
139111 try {
140112 try (InputStream tempStream = new FileInputStream (cachedFile )) {
141- type = URLConnection .guessContentTypeFromStream (tempStream );
113+ mimetype = URLConnection .guessContentTypeFromStream (tempStream );
142114 }
143115 } catch (Exception e ) {
144116 crashManager .log (e );
145117 }
146118 }
147119
148120 if (type != null ) {
149- extension = MimeTypeMap .getSingleton ().getExtensionFromMimeType (type );
121+ extension = MimeTypeMap .getSingleton ().getExtensionFromMimeType (mimetype );
150122 } else {
151- type = MimeTypeMap .getSingleton ().getMimeTypeFromExtension (options .fileExtension );
123+ mimetype = MimeTypeMap .getSingleton ().getMimeTypeFromExtension (options .fileExtension );
152124 }
153125
154126 if (extension != null ) {
155127 options .fileExtension = extension ;
156128 }
157- if (type != null ) {
158- options .fileType = type ;
129+ if (mimetype != null ) {
130+ options .fileType = mimetype ;
159131 }
160132
161- if ("inode/x-empty" .equals (type )) {
133+ if ("inode/x-empty" .equals (mimetype )) {
162134 throw new FileNotFoundException ();
163135 }
164136
@@ -183,20 +155,4 @@ public void loadSync(Options options) {
183155 callOnError (result , e );
184156 }
185157 }
186-
187- @ Override
188- public void close () {
189- super .close ();
190-
191- backgroundHandler .post (new Runnable () {
192- @ Override
193- public void run () {
194- try {
195- MagicApi .close ();
196- } catch (Throwable e ) {
197- crashManager .log (e );
198- }
199- }
200- });
201- }
202158}
0 commit comments