<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.doc;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.xpn.xwiki.web.Utils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.xwiki.environment.Environment;
import org.xwiki.store.UnexpectedException;

/**
 * The content of an attachment. Objects of this class hold the actual content which will be downloaded when a user
 * downloads an attachment.
 * 
 * @version $Id$
 */
public class XWikiAttachmentContent implements Cloneable
{
    /** The XWikiAttachment (attachment metadata) which this attachment content is associated with. */
    private XWikiAttachment attachment;

    /** True if the content is out of sync with the content stored in the database and thus needs to be saved. */
    private boolean isContentDirty;

    /** Storage which holds the actual content. */
    private FileItem file;

    /**
     * Constructor which clones an existing XWikiAttachmentContent. Used by {@link #clone()}.
     * 
     * @param original the XWikiAttachmentContent to clone.
     * @since 2.6M1
     */
    public XWikiAttachmentContent(XWikiAttachmentContent original)
    {
        this.file = original.file;
        this.attachment = original.attachment;
        this.isContentDirty = original.isContentDirty;
    }

    /**
     * Constructor with associated attachment specified.
     * 
     * @param attachment the attachment which this is the content for.
     */
    public XWikiAttachmentContent(XWikiAttachment attachment)
    {
        this();
        this.setAttachment(attachment);
    }

    /**
     * The default Constructor. For creating content which will be associated with an attachment later.
     */
    public XWikiAttachmentContent()
    {
        this.newFileItem();
    }

    /**
     * Set a new FileItem for storage.
     * 
     * @since 2.6M1
     */
    private void newFileItem()
    {
        final Environment env = Utils.getComponent(Environment.class);
        final File dir = new File(env.getTemporaryDirectory(), "attachment-cache");
        try {
            final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, dir);
            // This causes the temp file to be created.
            dfi.getOutputStream();
            // Make sure this file is marked for deletion on VM exit because DiskFileItem does not.
            dfi.getStoreLocation().deleteOnExit();
            this.file = dfi;
        } catch (IOException e) {
            throw new UnexpectedException("Failed to create new attachment temporary file.", e);
        }
    }

    /**
     * This is used so that Hibernate will associate this content with the right attachment (metadata).
     * 
     * @return the id of the attachment (metadata) which this content is associated with.
     */
    public long getId()
    {
        return this.attachment.getId();
    }

    /**
     * This function does nothing and exists only for Hibernate to be able to load a value which is not used.
     * 
     * @param id is ignored.
     */
    public void setId(long id)
    {<commit_msg>XWIKI-7748: Was not creating directory for attachment cache.<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.doc;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.xpn.xwiki.web.Utils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.xwiki.environment.Environment;
import org.xwiki.store.UnexpectedException;

/**
 * The content of an attachment. Objects of this class hold the actual content which will be downloaded when a user
 * downloads an attachment.
 * 
 * @version $Id$
 */
public class XWikiAttachmentContent implements Cloneable
{
    /** The XWikiAttachment (attachment metadata) which this attachment content is associated with. */
    private XWikiAttachment attachment;

    /** True if the content is out of sync with the content stored in the database and thus needs to be saved. */
    private boolean isContentDirty;

    /** Storage which holds the actual content. */
    private FileItem file;

    /**
     * Constructor which clones an existing XWikiAttachmentContent. Used by {@link #clone()}.
     * 
     * @param original the XWikiAttachmentContent to clone.
     * @since 2.6M1
     */
    public XWikiAttachmentContent(XWikiAttachmentContent original)
    {
        this.file = original.file;
        this.attachment = original.attachment;
        this.isContentDirty = original.isContentDirty;
    }

    /**
     * Constructor with associated attachment specified.
     * 
     * @param attachment the attachment which this is the content for.
     */
    public XWikiAttachmentContent(XWikiAttachment attachment)
    {
        this();
        this.setAttachment(attachment);
    }

    /**
     * The default Constructor. For creating content which will be associated with an attachment later.
     */
    public XWikiAttachmentContent()
    {
        this.newFileItem();
    }

    /**
     * Set a new FileItem for storage.
     * 
     * @since 2.6M1
     */
    private void newFileItem()
    {
        final Environment env = Utils.getComponent(Environment.class);
        final File dir = new File(env.getTemporaryDirectory(), "attachment-cache");
        try {
            if (!dir.mkdirs() && !dir.exists()) {
                throw new UnexpectedException("Failed to create directory for attachments " + dir);
            }
            final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, dir);
            // This causes the temp file to be created.
            dfi.getOutputStream();
            // Make sure this file is marked for deletion on VM exit because DiskFileItem does not.
            dfi.getStoreLocation().deleteOnExit();
            this.file = dfi;
        } catch (IOException e) {
            throw new UnexpectedException("Failed to create new attachment temporary file.", e);
        }
    }

    /**
     * This is used so that Hibernate will associate this content with the right attachment (metadata).
     * 
     * @return the id of the attachment (metadata) which this content is associated with.
     */
    public long getId()
    {
        return this.attachment.getId();
    }

    /**
     * This function does nothing and exists only for Hibernate to be able to load a value which is not used.
     * 
     * @param id is ignored.
     */
    public void setId(long id)
    {
{'fraction_non_alphanumeric': 0.07976176573630737, 'fraction_numerical': 0.00607263809412589, 'mean_word_length': 2.821508255243195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2775729', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2209, 'n_words': 887}