Skip to content

Commit

Permalink
Update card size to 180 - fix editor cap image bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincharles committed Mar 26, 2023
1 parent 5347709 commit a3201af
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 207 deletions.
3 changes: 2 additions & 1 deletion src/Api/getPortfolioActivityData.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
FROM course_content
WHERE doenetId = '$doenetId'
";
$temp = $sql;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
Expand All @@ -76,7 +77,6 @@
$message = 'You need permission to edita a portfolio activity.';
}
}

//Assume we are updating the activity and need the current settings
if ($success) {
$sql = "
Expand Down Expand Up @@ -112,6 +112,7 @@
'success' => $success,
'message' => $message,
'activityData' => $activityData,
'temp' => $temp,
];

http_response_code(200);
Expand Down
176 changes: 90 additions & 86 deletions src/Api/upload.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');

include "db_connection.php";
$jwtArray = include "jwtArray.php";
include 'db_connection.php';
$jwtArray = include 'jwtArray.php';
$userId = $jwtArray['userId'];

// include "randomId.php";
include "userQuotaBytesAvailable.php";
include "getFilename.php";
include "cidFromSHA.php";



$doenetId = mysqli_real_escape_string($conn,$_POST["doenetId"]);
include 'userQuotaBytesAvailable.php';
include 'getFilename.php';
include 'cidFromSHA.php';

$doenetId = mysqli_real_escape_string($conn, $_POST['doenetId']);

$success = true;
$msg = "";

$msg = '';

$uploads_dir = '../media/';

Expand All @@ -30,145 +26,153 @@
$error = $_FILES['file']['error'];
$size = $_FILES['file']['size'];
$original_file_name = $_FILES['file']['name'];
$description = ""; //Don't automatically fill with file name
$description = ''; //Don't automatically fill with file name
// $description = substr($original_file_name, 0, strrpos($original_file_name, "."));

$tmp_dest = $uploads_dir . getFileName('tmp_' . $random_id,$type);
$tmp_dest = $uploads_dir . getFileName('tmp_' . $random_id, $type);

//Test if user has permission to upload files

$canUpload = FALSE;
$canUpload = false;
$sql = "
SELECT canUpload
FROM user
WHERE userId = '$userId'
";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($row['canUpload'] == '1'){$canUpload = TRUE;}

if (!$canUpload){
$success = false;
$msg = "You don't have permission to upload files.";
if ($row['canUpload'] == '1') {
$canUpload = true;
}

//Test if user has space to upload files
if ($success){
list($userQuotaBytesAvailable,$quotaBytes) = getBytesAvailable($conn,$userId);

if ($userQuotaBytesAvailable - $size <= 0){
if (!$canUpload) {
$success = false;
$msg = "You don't have enough space in your quota to upload files.";
}
$msg = "You don't have permission to upload files.";
}

if ($success){
if ($size > 1000000){
$success = false;
$msg = "File is larger than 1MB limit.";
}
//Test if user has space to upload files
if ($success) {
list($userQuotaBytesAvailable, $quotaBytes) = getBytesAvailable(
$conn,
$userId
);

if ($userQuotaBytesAvailable - $size <= 0) {
$success = false;
$msg = "You don't have enough space in your quota to upload files.";
}
}

if ($success) {
if ($size > 1000000) {
$success = false;
$msg = 'File is larger than 1MB limit.';
}
}

$already_have_file = false;

if ($success){
move_uploaded_file($tmp_name, $tmp_dest);
$SHA = hash_file("sha256",$tmp_dest);
$cid = cidFromSHA($SHA);
$newFileName = getFileName($cid,$type);
$destination = $uploads_dir . $newFileName;
if ($success) {
move_uploaded_file($tmp_name, $tmp_dest);
$SHA = hash_file('sha256', $tmp_dest);
$cid = cidFromSHA($SHA);

$newFileName = getFileName($cid, $type);
$destination = $uploads_dir . $newFileName;

rename($tmp_dest,$destination);
rename($tmp_dest, $destination);

$mime_type = mime_content_type($destination);
$mime_type = mime_content_type($destination);

$width = 0;
$height = 0;
if ($mime_type == 'image/jpeg' || $mime_type == 'image/png'){
list($width,$height) = getimagesize($destination);
}
$width = 0;
$height = 0;
if ($mime_type == 'image/jpeg' || $mime_type == 'image/png') {
list($width, $height) = getimagesize($destination);
}

//Test if user already has this file in this activity
$sql = "
//Test if user already has this file in this activity
$sql = "
SELECT cid
FROM support_files
WHERE userId = '$userId'
AND cid = '$cid'
AND doenetId = '$doenetId'
";
$result = $conn->query($sql);
$result = $conn->query($sql);

if ($result->num_rows > 0){
if ($result->num_rows > 0) {
$already_have_file = true;
$success = false;
$msg = "Already have the file '$original_file_name' in this activity. Not used against your quota.";
}
$success = true;
$msg = "Already have the file '$original_file_name' in this activity. Not used against your quota.";
}
}


if ($success){
//Test if user has file in another activity
$sql = "
if ($success) {
//Test if user has file in another activity
$sql = "
SELECT cid
FROM support_files
WHERE userId = '$userId'
AND cid = '$cid'
AND doenetId != '$doenetId'
";
$result = $conn->query($sql);
$result = $conn->query($sql);

if ($result->num_rows > 0){
$already_have_file = true;
$msg = "Found the file in another activity. Not used against your quota.";
}
if ($result->num_rows > 0) {
$already_have_file = true;
$msg =
'Found the file in another activity. Not used against your quota.';
}
}


$escapedType = str_replace('\/', '/', $type);

if ($success && !$already_have_file){
//track upload for IPFS upload nanny to upload later
$sql = "
if ($success && !$already_have_file) {
//track upload for IPFS upload nanny to upload later
$sql = "
INSERT INTO ipfs_to_upload
(cid,fileType,sizeInBytes,timestamp)
VALUES
('$cid','$escapedType','$size',CONVERT_TZ(NOW(), @@session.time_zone, '+00:00'))
";
$result = $conn->query($sql);
$result = $conn->query($sql);
}

if ($success){
$sql = "
if ($success) {
$sql = "
INSERT INTO support_files
(userId,cid,doenetId,fileType,description,asFileName,sizeInBytes,widthPixels,heightPixels,timestamp)
VALUES
('$userId','$cid','$doenetId','$escapedType','$description','$original_file_name','$size','$width','$height',CONVERT_TZ(NOW(), @@session.time_zone, '+00:00'))
";
$result = $conn->query($sql);
$result = $conn->query($sql);
}
if ($success){
// //TODO: test at the top as well for over quota
list($userQuotaBytesAvailable,$quotaBytes) = getBytesAvailable($conn,$userId);
if ($success) {
// //TODO: test at the top as well for over quota
list($userQuotaBytesAvailable, $quotaBytes) = getBytesAvailable(
$conn,
$userId
);
}

// set response code - 200 OK
http_response_code(200);

$response_arr = array("success" => $success,
"cid" => $cid,
"fileName" => $newFileName,
"description" => $description,
"asFileName" => $original_file_name,
"width" => $width,
"height" => $height,
"msg" => $msg,
"userQuotaBytesAvailable" => $userQuotaBytesAvailable);
$response_arr = [
'success' => $success,
'cid' => $cid,
'fileName' => $newFileName,
'description' => $description,
'asFileName' => $original_file_name,
'width' => $width,
'height' => $height,
'msg' => $msg,
'userQuotaBytesAvailable' => $userQuotaBytesAvailable,
];

// make it json format
echo json_encode($response_arr);

$conn->close();

?>
?>
40 changes: 32 additions & 8 deletions src/Tools/_framework/MenuPanelCaps/PortfilioEditorInfoCap.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import { Box, Image } from '@chakra-ui/react';
import React from 'react';
import { useLoaderData } from 'react-router';

export async function loader(){
// export async function loader({params}){
let params = new URL(document.location).searchParams;
let doenetId = params.get("doenetId"); // is the string "Jonathan Smith".
const response = await fetch(`/api/getPortfolioActivityData.php?doenetId=${doenetId}`);
const data = await response.json();

// let params = new URL(document.location).searchParams;
// let search = new URL(document.location).search;
// console.log("search",search)
// let doenetId = params.get("doenetId");
let afterDoenetId = document.location.href?.split('&doenetId=')[1];
let doenetId = afterDoenetId?.split('&pageId=')[0];
//Somehow it still has the url which got us here
if (doenetId == undefined){
doenetId = document.location.href?.split('/')[4];
}
const response = await fetch(`/api/getPortfolioActivityData.php?doenetId=${doenetId}`);
const data = await response.json();

return data.activityData;
}

export default function PortfilioEditorInfoCap(){
let data = useLoaderData();

return <>
<div style={{ position: "relative", width: "100%", height:"124px", overflow: "hidden"}}>
<img src={data.imagePath} alt="Activity Thumbnail"/>
</div>
<Box
height="130px"
width="100%"
// width="180px"
background="black"
overflow="hidden"
// border="2px solid #949494"
// borderRadius= "6px"
margin= "auto"
>
<Image
width="100%"
height="100%"
objectFit="contain"
src={data.imagePath}
alt="Activity Image"
/>
</Box>
<div style={{ marginBottom: "5px",padding:'1px 5px' }}>Portfolio Activity Editor</div>
{/* <div style={{ marginBottom: "5px",padding:'1px 5px' }}>{data.label}</div> */}
</>
Expand Down
Loading

0 comments on commit a3201af

Please sign in to comment.