svnno****@sourc*****
svnno****@sourc*****
2007年 11月 28日 (水) 18:57:35 JST
Revision: 260 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=bbs2ch&view=rev&rev=260 Author: flyson Date: 2007-11-28 18:57:35 +0900 (Wed, 28 Nov 2007) Log Message: ----------- b2rDownloaderでダウンロードするときに一時ファイルを作成するようにした Modified Paths: -------------- trunk/bbs2chreader/chrome/content/bbs2chreader/bbsmenu/page.xul trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js -------------- next part -------------- Modified: trunk/bbs2chreader/chrome/content/bbs2chreader/bbsmenu/page.xul =================================================================== --- trunk/bbs2chreader/chrome/content/bbs2chreader/bbsmenu/page.xul 2007-11-25 11:58:46 UTC (rev 259) +++ trunk/bbs2chreader/chrome/content/bbs2chreader/bbsmenu/page.xul 2007-11-28 09:57:35 UTC (rev 260) @@ -12,10 +12,11 @@ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> -<script type="application/x-javascript" src="chrome://bbs2chreader/content/lib/downloader.js"/> -<script type="application/x-javascript" src="chrome://bbs2chreader/content/lib/clipboard.js"/> -<script type="application/x-javascript" src="chrome://bbs2chreader/content/bbsmenu/treeview.js"/> -<script type="application/x-javascript" src="chrome://bbs2chreader/content/bbsmenu/page.js"/> +<script type="application/javascript;version=1.7" src="chrome://bbs2chreader/content/lib/xpc.js"/> +<script type="application/javascript;version=1.7" src="chrome://bbs2chreader/content/lib/downloader.js"/> +<script type="application/javascript;version=1.7" src="chrome://bbs2chreader/content/lib/clipboard.js"/> +<script type="application/javascript;version=1.7" src="chrome://bbs2chreader/content/bbsmenu/treeview.js"/> +<script type="application/javascript;version=1.7" src="chrome://bbs2chreader/content/bbsmenu/page.js"/> <popupset> <popup id="popBbsMenuContext" Modified: trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js =================================================================== --- trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js 2007-11-25 11:58:46 UTC (rev 259) +++ trunk/bbs2chreader/chrome/content/bbs2chreader/lib/downloader.js 2007-11-28 09:57:35 UTC (rev 260) @@ -139,11 +139,24 @@ this._httpChannel = null; } - var ioService = Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService); - var bbs2chService = Components.classes["@mozilla.org/bbs2ch-service;1"] - .getService(Components.interfaces.nsIBbs2chService); + var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); + var b2rService = Cc["@bbs2ch.sourceforge.jp/b2r-global-service;1"].getService(Ci.b2rIGlobalService); + try{ + this._file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); + this._file.initWithPath(this.filePath); + + this._tempFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); + this._tempFile.initWithPath(this._file.path + ".tmp"); + this._tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + if(!this._tempFile.exists()){ + this._tempFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + } + }catch(ex){ + this.onError(context, context.ERROR_BAD_FILE_PATH); + return; + } + // nsIURI の作成 try{ var fromURI = ioService.newURI(this.urlSpec, null, null); @@ -152,14 +165,14 @@ return; } - this._httpChannel = bbs2chService.getHttpChannel(fromURI); + this._httpChannel = b2rService.getHttpChannel(fromURI); this._httpChannel.requestMethod = "GET"; this._httpChannel.redirectionLimit = 0; // 302 等のリダイレクトを行わない - this._httpChannel.loadFlags = this._httpChannel.LOAD_BYPASS_CACHE; + this._httpChannel.loadFlags = Ci.nsIHttpChannel.LOAD_BYPASS_CACHE; try{ - this._listener._context = this; - this._httpChannel.asyncOpen(this._listener, null); + this.wrappedJSObject = this; + this._httpChannel.asyncOpen(this._listener, this); this._loading = true; }catch(ex){ this.onError(this, this.ERROR_FAILURE); @@ -169,11 +182,21 @@ _listener: { onStartRequest: function(aRequest, aContext){ - this._bInputStream = Components.classes["@mozilla.org/binaryinputstream;1"] - .createInstance(Components.interfaces.nsIBinaryInputStream); - this._data = new Array(); + var context = aContext.wrappedJSObject; + + // 0x02=PR_WRONLY; 0x08=PR_CREATE_FILE; + // 0x10=PR_APPEND; 0x20=PR_TRUNCATE; + var fileOutputStream = Cc["@mozilla.org/network/file-output-stream;1"] + .createInstance(Ci.nsIFileOutputStream); + fileOutputStream.init(context._tempFile, 0x02|0x08|0x20, 0666, 0); + + this._outputStream = Cc["@mozilla.org/network/buffered-output-stream;1"] + .createInstance(Ci.nsIBufferedOutputStream); + this._outputStream.init(fileOutputStream, 1024*16); }, onDataAvailable: function (aRequest, aContext, aInputStream, aOffset, aCount){ + var context = aContext.wrappedJSObject; + aRequest.QueryInterface(Components.interfaces.nsIHttpChannel); var httpStatus = aRequest.responseStatus; // 必要な情報がないなら終了 @@ -181,35 +204,37 @@ if(aCount == 0) return; if(aRequest.contentLength > 0){ - var context = this._context; percentage = Math.floor((aOffset * 100.0) / aRequest.contentLength); context.onProgressChange(context, percentage); } - this._bInputStream.setInputStream(aInputStream); - this._data.push(this._bInputStream.readBytes(aCount)); + this._outputStream.writeFrom(aInputStream, aCount); }, onStopRequest: function(aRequest, aContext, aStatus){ + var context = aContext.wrappedJSObject; + context._loading = false; + + if(!this._outputStream) return; + this._outputStream.close(); + this._outputStream = null; + // nsNetError.h const NS_ERROR_MODULE_NETWORK = 2152398848; const NS_ERROR_NET_TIMEOUT = NS_ERROR_MODULE_NETWORK + 14; const NS_ERROR_UNKNOWN_HOST = NS_ERROR_MODULE_NETWORK + 30; const NS_ERROR_DOCUMENT_NOT_CACHED = NS_ERROR_MODULE_NETWORK + 70; - var context = this._context; - var data = this._data.join(""); - this._data = null; - try{ aRequest.QueryInterface(Components.interfaces.nsIHttpChannel); var httpStatus = aRequest.responseStatus; if(httpStatus==200 || httpStatus==206){ - bbs2chService = Components.classes["@mozilla.org/bbs2ch-service;1"] - .getService(Components.interfaces.nsIBbs2chService); - var writed = bbs2chService.writeFile(context.filePath, data, false); - if(!writed){ - this.onError(context, context.ERROR_BAD_FILE_PATH); - return; + try{ + if(context._file.exists()){ + context._file.remove(false); + } + context._tempFile.moveTo(context._file.parent, context._file.leafName); + }catch(ex){ + context.onError(context, context.ERROR_BAD_FILE_PATH); } } context.onStop(context, httpStatus); @@ -241,10 +266,17 @@ try{ this._httpChannel.cancel(0); this._httpChannel = null; + if(this._listener._outputStream){ + this._listener._outputStream.close(); + this._listener._outputStream = null; + } + if(this._tempFile.exists()){ + this._tempFile.remove(false); + } }catch(ex){} - - if(!aSilent) this.onError(this.ERROR_CANCEL); - + + if(!aSilent) this.onError(this, this.ERROR_CANCEL); + this._loading = false; },