11use crate :: bot_types:: { _Context as Context , Error } ;
22use image;
33use poise:: serenity_prelude as serenity;
4+ use serenity:: all:: Message ;
5+ use serenity:: builder:: CreateMessage ;
46use std:: path:: { Path , PathBuf } ;
57use tokio:: io:: AsyncWriteExt ;
68
@@ -11,6 +13,7 @@ const ROTATE_FOLDER: &str = "rotated";
1113#[ poise:: command( prefix_command) ]
1214pub async fn rotate ( ctx : Context < ' _ > ) -> Result < ( ) , Error > {
1315 let msg = ctx. channel_id ( ) . message ( & ctx. http ( ) , ctx. id ( ) ) . await ?;
16+ // rotate_image(msg.clone()).await;
1417
1518 if msg. referenced_message . is_none ( ) {
1619 ctx. reply ( "Nothing to rotate..." )
@@ -99,6 +102,10 @@ async fn get_and_save_picture(link: &str, file_path: &Path) -> Result<bool, Erro
99102 if !response. status ( ) . is_success ( ) {
100103 return Err ( Error :: from ( response. error_for_status ( ) . unwrap_err ( ) ) ) ;
101104 }
105+ const MAX_SIZE : u64 = 50_000_000 ;
106+ if response. content_length ( ) . is_none_or ( |len| len > MAX_SIZE ) {
107+ return Err ( Error :: from ( "File to large." ) ) ;
108+ }
102109 let mut file = tokio:: fs:: File :: create ( file_path) . await ?;
103110 let content = response. bytes ( ) . await ?;
104111 file. write_all ( & content) . await ?;
@@ -114,3 +121,41 @@ async fn rotate_image_and_save(file_path: &Path, save_path: &Path) {
114121 let rot_img = img. rotate180 ( ) ;
115122 rot_img. save ( save_path) . expect ( "Error Saving Image" ) ;
116123}
124+
125+ pub async fn rotate_image_directly (
126+ http : & serenity:: Http ,
127+ channel_id : serenity:: ChannelId ,
128+ msg : & Message ,
129+ ) -> Result < ( ) , Error > {
130+ if !matches ! (
131+ msg. attachments
132+ . first( )
133+ . and_then( |a| a. content_type. as_deref( ) ) ,
134+ Some ( "image/png" | "image/jpeg" )
135+ ) {
136+ return Err ( Error :: from ( "Not a valid image" ) ) ;
137+ }
138+ check_folders ( ) ;
139+ let attachments_link = msg. attachments . first ( ) . unwrap ( ) . proxy_url . clone ( ) ;
140+ let content_type = msg
141+ . attachments
142+ . first ( )
143+ . unwrap ( )
144+ . content_type
145+ . clone ( )
146+ . unwrap ( ) ;
147+
148+ let file_name = generate_file_name ( msg. id . to_string ( ) , & content_type) ;
149+
150+ let original_file_path = PathBuf :: from ( ROOT ) . join ( ORIGINAL_FOLDER ) . join ( & file_name) ;
151+ get_and_save_picture ( & attachments_link, & original_file_path) . await ?;
152+
153+ let rotate_file_path = PathBuf :: from ( ROOT ) . join ( ROTATE_FOLDER ) . join ( & file_name) ;
154+ rotate_image_and_save ( & original_file_path, & rotate_file_path) . await ;
155+
156+ let attachment = serenity:: CreateAttachment :: path ( & rotate_file_path) . await ?;
157+ let builder = CreateMessage :: new ( ) . add_file ( attachment) ;
158+ channel_id. send_message ( http, builder) . await ?;
159+
160+ Ok ( ( ) )
161+ }
0 commit comments