<commit_before>/**
 * 
 */
package hudson.plugins.emailext;

import hudson.FilePath;
import hudson.Util;
import hudson.FilePath.FileCallable;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;

/**
 * @author acearl
 *
 */
public class AttachmentUtils implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String attachmentsPattern;
	
	public AttachmentUtils(String attachmentsPattern) {
		this.attachmentsPattern = attachmentsPattern;
	}
	
	/**
     * Provides a datasource wrapped around the FilePath class to
     * allow access to remote files (on slaves).
     * @author acearl
     *
     */
    private static class FilePathDataSource implements DataSource {
    	private FilePath file;

    	public FilePathDataSource(FilePath file) {
    		this.file = file;
    	}
    	
		public InputStream getInputStream() throws IOException {
			return file.read();
		}

		public OutputStream getOutputStream() throws IOException {
			throw new IOException("Unsupported");
		}

		public String getContentType() {
			return MimetypesFileTypeMap.getDefaultFileTypeMap()
					.getContentType(file.getName());
		}

		public String getName() {
			return file.getName();
		}    	
    }
    
    private List<MimeBodyPart> getAttachments(final AbstractBuild<?, ?> build, final BuildListener listener)
    		throws MessagingException, InterruptedException, IOException {
    	List<MimeBodyPart> attachments = null;
    	FilePath ws = build.getWorkspace();
    	long totalAttachmentSize = 0;
		long maxAttachmentSize = 
				ExtendedEmailPublisher.DESCRIPTOR.getMaxAttachmentSize();
    	if(ws == null) {
    		listener.error("Error: No workspace found!");
    	} else if(attachmentsPattern != null && attachmentsPattern.trim().length() > 0) {
    		attachments = new ArrayList<MimeBodyPart>();
    		List<FilePath> files = ws.act(new FileCallable<List<FilePath>>() {
				public List<FilePath> invoke(File baseDir, VirtualChannel channel)
						throws IOException {
					List<FilePath> results = new ArrayList<FilePath>();
					FileSet src = Util.createFileSet(baseDir, attachmentsPattern);
	                DirectoryScanner ds = src.getDirectoryScanner();
	                for( String f : ds.getIncludedFiles() ) {
	                	final File file = new File(baseDir, f);
	                	if(file.isFile()) {
	                		results.add(new FilePath(file));
	                	} else {
	                		listener.getLogger().println("Skipping `" 
	                				+ file.getName() + "' - not a file");		
	                	}
	                }	                	
					return results;
				}
				private static final long serialVersionUID = 1L;
    		});    		
    	
	    	for(FilePath file : files) {
		    	if(maxAttachmentSize > 0 && 
		    			(totalAttachmentSize + file.length()) >= maxAttachmentSize) {
		    		listener.getLogger().println("Skipping `" + file.getName() 
		    				+ "' ("+ file.length() + 
		    				" bytes) - too large for maximum attachments size");
		    		continue;
		    	}           
	    	
		    	MimeBodyPart attachmentPart = new MimeBodyPart();
		    	FilePathDataSource fileDataSource = new FilePathDataSource(file);
			
				try {
					attachmentPart.setDataHandler(new DataHandler(fileDataSource));
					attachmentPart.setFileName(file.getName());
					attachments.add(attachmentPart);	        	
					totalAttachmentSize += file.length();
				} catch(MessagingException e) {
					listener.getLogger().println("Error adding `" + 
							file.getName() + "' as attachment - " + 
							e.getMessage());
				}
	    	}
    	}    	
    	return attachments;
    }
    <commit_msg>Reduced complexity of the method to grab files to use FilePath.list<commit_after>/**
 * 
 */
package hudson.plugins.emailext;

import hudson.FilePath;
import hudson.Util;
import hudson.FilePath.FileCallable;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;

/**
 * @author acearl
 *
 */
public class AttachmentUtils implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String attachmentsPattern;
	
	public AttachmentUtils(String attachmentsPattern) {
		this.attachmentsPattern = attachmentsPattern;
	}
	
	/**
     * Provides a datasource wrapped around the FilePath class to
     * allow access to remote files (on slaves).
     * @author acearl
     *
     */
    private static class FilePathDataSource implements DataSource {
    	private FilePath file;

    	public FilePathDataSource(FilePath file) {
    		this.file = file;
    	}
    	
		public InputStream getInputStream() throws IOException {
			return file.read();
		}

		public OutputStream getOutputStream() throws IOException {
			throw new IOException("Unsupported");
		}

		public String getContentType() {
			return MimetypesFileTypeMap.getDefaultFileTypeMap()
					.getContentType(file.getName());
		}

		public String getName() {
			return file.getName();
		}    	
    }
    
    private List<MimeBodyPart> getAttachments(final AbstractBuild<?, ?> build, final BuildListener listener)
    		throws MessagingException, InterruptedException, IOException {
    	List<MimeBodyPart> attachments = null;
    	FilePath ws = build.getWorkspace();
    	long totalAttachmentSize = 0;
		long maxAttachmentSize = 
				ExtendedEmailPublisher.DESCRIPTOR.getMaxAttachmentSize();
    	if(ws == null) {
    		listener.error("Error: No workspace found!");
    	} else if(attachmentsPattern != null && attachmentsPattern.trim().length() > 0) {
    		attachments = new ArrayList<MimeBodyPart>();
    		FilePath[] files = ws.list(attachmentsPattern);    		
    	
	    	for(FilePath file : files) {
		    	if(maxAttachmentSize > 0 && 
		    			(totalAttachmentSize + file.length()) >= maxAttachmentSize) {
		    		listener.getLogger().println("Skipping `" + file.getName() 
		    				+ "' ("+ file.length() + 
		    				" bytes) - too large for maximum attachments size");
		    		continue;
		    	}           
	    	
		    	MimeBodyPart attachmentPart = new MimeBodyPart();
		    	FilePathDataSource fileDataSource = new FilePathDataSource(file);
			
				try {
					attachmentPart.setDataHandler(new DataHandler(fileDataSource));
					attachmentPart.setFileName(file.getName());
					attachments.add(attachmentPart);	        	
					totalAttachmentSize += file.length();
				} catch(MessagingException e) {
					listener.getLogger().println("Error adding `" + 
							file.getName() + "' as attachment - " + 
							e.getMessage());
				}
	    	}
    	}    	
    	return attachments;
    }
    
{'fraction_non_alphanumeric': 0.10094265514532601, 'fraction_numerical': 0.001178318931657502, 'mean_word_length': 3.071961620469083, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4151365', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2141, 'n_words': 475}