How to move image thumbnails into custom folder for custom post type

Use bellow filter in the functions.php

<?php
add_filter( 'upload_dir', function($args) {
    if( !isset($_REQUEST['post_id']) ) {
        return $args;
    }

    $post_type_name = get_post_type( $_REQUEST['post_id'] );

    if( $post_type_name="custom_post_type" ) {
        // Set the new path depends on current post_type
        $newdir="https://wordpress.stackexchange.com/" . $post_type_name;
        $args['path']    = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
        $args['url']     = str_replace( $args['subdir'], '', $args['url'] );
        $args['subdir']  = $newdir;
        $args['path']   .= $newdir;
        $args['url']    .= $newdir;
        return $args;
    }

    return $args;
} ); ?>

https://gist.github.com/mostafasoufi/c525572d9b4404808b0a7bbf14f09ffa