<s> import json <EOL> from Util import Util <EOL> import Config <EOL> import base64 <EOL> from errors import * <EOL> class Store : <EOL> @ staticmethod <EOL> def GET ( svc , session , params , action ) : <EOL> raise WrongArgs ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def POST ( svc , session , params , action ) : <EOL> if ( session == None ) : raise Unauthorized ( '<STR_LIT>' ) <EOL> if not session . CheckScope ( '<STR_LIT>' ) : raise NoPerm ( "<STR_LIT>" ) <EOL> if ( action == "<STR_LIT>" ) : <EOL> item = svc . get_str ( params , '<STR_LIT>' ) <EOL> content = svc . get_str ( params , '<STR_LIT:content>' ) <EOL> store_id = Store . new ( item , content ) <EOL> result = { } <EOL> result [ '<STR_LIT:id>' ] = store_id <EOL> svc . writedata ( json . dumps ( result ) ) <EOL> else : <EOL> raise WrongArgs ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def new ( item , content ) : <EOL> if ( item != '<STR_LIT>' ) : <EOL> raise WrongArgs ( "<STR_LIT>" ) <EOL> try : <EOL> data = base64 . b64decode ( content ) <EOL> except : <EOL> raise WrongArgs ( "<STR_LIT>" ) <EOL> filename = Util . RandomStr ( <NUM_LIT:16> ) <EOL> filepath = Store . path_from_id ( filename ) <EOL> fp = None <EOL> try_count = <NUM_LIT:16> <EOL> while ( try_count > <NUM_LIT:0> ) : <EOL> while ( try_count > <NUM_LIT:0> ) : <EOL> try : <EOL> fp = open ( filepath , "<STR_LIT:rb>" ) <EOL> filename = Util . RandomStr ( <NUM_LIT:16> ) <EOL> filepath = Store . path_from_id ( filename ) <EOL> except : <EOL> break <EOL> try_count -= <NUM_LIT:1> <EOL> try : <EOL> with open ( filepath , "<STR_LIT:wb>" ) as fp : <EOL> fp . write ( data ) <EOL> return filename <EOL> except : <EOL> filename = Util . RandomStr ( <NUM_LIT:16> ) <EOL> filepath = Store . path_from_id ( filename ) <EOL> try_count -= <NUM_LIT:1> <EOL> raise ServerError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def path_from_id ( id ) : <EOL> return Config . BBS_ROOT + "<STR_LIT>" + id + "<STR_LIT>" <EOL> @ staticmethod <EOL> def verify_id ( id ) : <EOL> if ( not Util . CheckStr ( id ) ) : <EOL> return False <EOL> if ( len ( id ) != <NUM_LIT:16> ) : <EOL> return False <EOL> try : <EOL> filepath = Store . path_from_id ( id ) <EOL> with open ( filepath , "<STR_LIT:rb>" ) : <EOL> pass <EOL> except : <EOL> return False <EOL> return True </s>
