34 const FILE_INPUT_NAME =
'fileInput';
37 public function __construct()
39 parent::__construct(self::NAME, array(
40 self::TITLE => _(
"Upload a New File"),
41 self::MENU_LIST =>
"Upload::From File",
42 self::DEPENDENCIES => array(
"agent_unpack",
"showjobs"),
54 $vars[
'fileInputName'] = self::FILE_INPUT_NAME;
55 return $this->
render(
"upload_file.html.twig", $this->mergeWithDefault($vars));
66 define(
"UPLOAD_ERR_EMPTY", 5);
67 define(
"UPLOAD_ERR_INVALID_FOLDER_PK", 100);
68 define(
"UPLOAD_ERR_RESEND", 200);
69 $uploadErrors = array(
70 UPLOAD_ERR_OK => _(
"No errors."),
71 UPLOAD_ERR_INI_SIZE => _(
"Larger than upload_max_filesize ") . ini_get(
'upload_max_filesize'),
72 UPLOAD_ERR_FORM_SIZE => _(
"Larger than form MAX_FILE_SIZE."),
73 UPLOAD_ERR_PARTIAL => _(
"Partial upload."),
74 UPLOAD_ERR_NO_FILE => _(
"No file selected."),
75 UPLOAD_ERR_NO_TMP_DIR => _(
"No temporary directory."),
76 UPLOAD_ERR_CANT_WRITE => _(
"Can't write to disk."),
77 UPLOAD_ERR_EXTENSION => _(
"File upload stopped by extension."),
78 UPLOAD_ERR_EMPTY => _(
"File is empty or you don't have permission to read the file."),
79 UPLOAD_ERR_INVALID_FOLDER_PK => _(
"Invalid Folder."),
80 UPLOAD_ERR_RESEND => _(
"This seems to be a resent file.")
83 $folderId = intval($request->get(self::FOLDER_PARAMETER_NAME));
84 $description = stripslashes($request->get(self::DESCRIPTION_INPUT_NAME));
85 $description = $this->basicShEscaping($description);
86 $uploadedFile = $request->files->get(self::FILE_INPUT_NAME);
88 if ($uploadedFile === null) {
89 return array(
false, $uploadErrors[UPLOAD_ERR_NO_FILE], $description);
92 if ($request->getSession()->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)
93 != $request->get(self::UPLOAD_FORM_BUILD_PARAMETER_NAME)) {
94 return array(
false, $uploadErrors[UPLOAD_ERR_RESEND], $description);
97 if ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
98 return array(
false, $uploadErrors[UPLOAD_ERR_EMPTY], $description);
99 }
else if ($uploadedFile->getSize() >= UploadedFile::getMaxFilesize()) {
100 return array(
false, $uploadErrors[UPLOAD_ERR_INI_SIZE] .
101 _(
" is really ") . $uploadedFile->getSize() .
" bytes.", $description);
104 if (empty($folderId)) {
105 return array(
false, $uploadErrors[UPLOAD_ERR_INVALID_FOLDER_PK], $description);
108 if (!$uploadedFile->isValid()) {
109 return array(
false, $uploadedFile->getErrorMessage(), $description);
112 $originalFileName = $uploadedFile->getClientOriginalName();
113 $originalFileName = $this->basicShEscaping($originalFileName);
115 $public = $request->get(
'public');
119 $uploadMode = (1 << 3);
122 $uploadId =
JobAddUpload($userId, $groupId, $originalFileName,
123 $originalFileName, $description, $uploadMode, $folderId, $publicPermission);
124 if (empty($uploadId)) {
125 return array(
false, _(
"Failed to insert upload record"), $description);
129 $uploadedTempFile = $uploadedFile->move($uploadedFile->getPath(),
130 $uploadedFile->getFilename() .
'-uploaded')->getPathname();
131 }
catch (FileException $e) {
132 return array(
false, _(
"Could not save uploaded file"), $description);
135 $projectGroup = $GLOBALS[
'SysConf'][
'DIRECTORIES'][
'PROJECTGROUP'] ?:
'fossy';
136 $wgetAgentCall =
"$MODDIR/wget_agent/agent/wget_agent -C -g $projectGroup -k $uploadId '$uploadedTempFile' -c '$SYSCONFDIR'";
137 $wgetOutput = array();
138 exec($wgetAgentCall, $wgetOutput, $wgetReturnValue);
139 unlink($uploadedTempFile);
141 if ($wgetReturnValue != 0) {
142 $message = implode(
' ', $wgetOutput);
143 if (empty($message)) {
144 $message = _(
"File upload failed. Error:") . $wgetReturnValue;
146 return array(
false, $message, $description);
149 $message = $this->postUploadAddJobs($request, $originalFileName, $uploadId);
151 return array(
true, $message, $description, $uploadId);
static getUserId()
Get the current user's id.
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.
handleView(Request $request, $vars)
static getGroupId()
Get the current user's group id.
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
Insert a new upload record, and update the foldercontents table.
handleUpload(Request $request)
Process the upload request.