Hey there, I have an application in which file uploads are working perfectly well. I have a module which acts upon the uploaded file (reads a few bytes to check validity) and then seeks back to 0. Now, when I use shutil.copyfileobj to copy this to a named temporary file for popen to use, it leaves out a few bytes from the end of the original file. What could be the reason for this? And, if this is some internal Python problem or something, how can I change the default_stream_factory in werkzeug.formparser which apparently handles this bit? Instead of TemporaryFile, it could use a NamedTemporaryFile. Then I could freely get the filename and would not have any need to copy it to a new file. Any way to hook into this via Flask? Regards, Ishbir Singh
Hi, On 9/14/11 7:50 AM, Ishbir Singh wrote: > And, if this is some internal Python problem or something, how can I > change thedefault_stream_factory in werkzeug.formparser which apparently > handles this bit? Instead of TemporaryFile, it could use a > NamedTemporaryFile. Then I could freely get the filename and would not > have any need to copy it to a new file. Any way to hook into this via Flask? class MyRequest(flask.Request): def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None): return MakeFileHere() app = Flask(__name__) app.request_class = MyRequest Regards, Armin
Hey, Thankyou! That works. :) On 15 September 2011 01:10, Armin Ronacher <armin.ronacher@active-4.com>wrote: > Hi, > > On 9/14/11 7:50 AM, Ishbir Singh wrote: > > And, if this is some internal Python problem or something, how can I > > change thedefault_stream_factory in werkzeug.formparser which apparently > > handles this bit? Instead of TemporaryFile, it could use a > > NamedTemporaryFile. Then I could freely get the filename and would not > > have any need to copy it to a new file. Any way to hook into this via > Flask? > class MyRequest(flask.Request): > def _get_file_stream(self, total_content_length, content_type, > filename=None, content_length=None): > return MakeFileHere() > > app = Flask(__name__) > app.request_class = MyRequest > > > Regards, > Armin > -- Regards, Ishbir Singh
Hi, CAn you share the code snippet please. I am looking for the same type except where I need to read a file into a buffer, without having to upload it on the server. ref. http://flask.pocoo.org/mailinglist/archive/2011/9/5/file-reading-problem/ On 09/14/2011 11:20 AM, Ishbir Singh wrote: > Hey there, > > I have an application in which file uploads are working perfectly > well. I have a module which acts upon the uploaded file (reads a few > bytes to check validity) and then seeks back to 0. Now, when I use > shutil.copyfileobj to copy this to a named temporary file for popen to > use, it leaves out a few bytes from the end of the original file. What > could be the reason for this? > > And, if this is some internal Python problem or something, how can I > change thedefault_stream_factory in werkzeug.formparser which > apparently handles this bit? Instead of TemporaryFile, it could use a > NamedTemporaryFile. Then I could freely get the filename and would not > have any need to copy it to a new file. Any way to hook into this via > Flask? > > Regards, > Ishbir Singh > -- rgds...Shantala
Hey, I don't quite get you. From the link you gave me, I looked up the snippets of your code. No offence, but your code is literally like spaghetti. Why are you re-inventing the wheel when you have great extensions like Flask-Mail ( http://packages.python.org/flask-mail/)? And moreover, Flask (Werkzeug, actually) saves file with size less than 500 KB in StringIO buffer and if size is more, then in a temporary file. Lookup https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L33 On 14 September 2011 11:47, shantala <shantala@yukthi.com> wrote: > Hi, > CAn you share the code snippet please. I am looking for the same type > except where I need to read a file into a buffer, without having to > upload it on the server. > ref. > http://flask.pocoo.org/mailinglist/archive/2011/9/5/file-reading-problem/ > > On 09/14/2011 11:20 AM, Ishbir Singh wrote: > > Hey there, > > > > I have an application in which file uploads are working perfectly > > well. I have a module which acts upon the uploaded file (reads a few > > bytes to check validity) and then seeks back to 0. Now, when I use > > shutil.copyfileobj to copy this to a named temporary file for popen to > > use, it leaves out a few bytes from the end of the original file. What > > could be the reason for this? > > > > And, if this is some internal Python problem or something, how can I > > change thedefault_stream_factory in werkzeug.formparser which > > apparently handles this bit? Instead of TemporaryFile, it could use a > > NamedTemporaryFile. Then I could freely get the filename and would not > > have any need to copy it to a new file. Any way to hook into this via > > Flask? > > > > Regards, > > Ishbir Singh > > > > > -- > rgds...Shantala > > > -- Regards, Ishbir Singh
Hi, Thanx, I am new to flask or rather programming. Got to know that Flask can save file less than 500 KB in StringIO only now. On 09/14/2011 12:01 PM, Ishbir Singh wrote: > Hey, > > I don't quite get you. From the link you gave me, I looked up the > snippets of your code. No offence, but your code is literally like > spaghetti. Why are you re-inventing the wheel when you have great > extensions like Flask-Mail (http://packages.python.org/flask-mail/)? > > And moreover, Flask (Werkzeug, actually) saves file with size less > than 500 KB in StringIO buffer and if size is more, then in a > temporary file. Lookup > https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/formparser.py#L33 > > > On 14 September 2011 11:47, shantala <shantala@yukthi.com > <mailto:shantala@yukthi.com>> wrote: > > Hi, > CAn you share the code snippet please. I am looking for the same type > except where I need to read a file into a buffer, without having to > upload it on the server. > ref. > http://flask.pocoo.org/mailinglist/archive/2011/9/5/file-reading-problem/ > > On 09/14/2011 11:20 AM, Ishbir Singh wrote: > > Hey there, > > > > I have an application in which file uploads are working perfectly > > well. I have a module which acts upon the uploaded file (reads a few > > bytes to check validity) and then seeks back to 0. Now, when I use > > shutil.copyfileobj to copy this to a named temporary file for > popen to > > use, it leaves out a few bytes from the end of the original > file. What > > could be the reason for this? > > > > And, if this is some internal Python problem or something, how can I > > change thedefault_stream_factory in werkzeug.formparser which > > apparently handles this bit? Instead of TemporaryFile, it could > use a > > NamedTemporaryFile. Then I could freely get the filename and > would not > > have any need to copy it to a new file. Any way to hook into > this via > > Flask? > > > > Regards, > > Ishbir Singh > > > > > -- > rgds...Shantala > > > > > > -- > Regards, > Ishbir Singh >