Skip to content

Commit

Permalink
"Made several minor improvements and bug fixes."
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenkingston committed Sep 8, 2020
1 parent 15d2f34 commit e91ab3a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 46 deletions.
3 changes: 1 addition & 2 deletions css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ body
}

.file-manager {
height: 100%;
height: calc(100% - 70px);
background: azure;
position: fixed;
top:70px;
Expand Down Expand Up @@ -291,7 +291,6 @@ body
.fileSize{
display: none
}

}

#about {
Expand Down
62 changes: 47 additions & 15 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ let activeStorageID = 0;
async function main()
{
let mtpDevice = await MTPDeviceInit();
let fileObjects = null;

let storageObjects = await getStorageIDS(mtpDevice);
console.log(storageObjects);

activeStorageID = storageObjects[0].storageID;
fileObjects = await getFileObjects(mtpDevice, activeStorageID)
console.log(fileObjects);
let fileObjects = await getFileObjects(mtpDevice, activeStorageID)

refreshUI(fileObjects, storageObjects[0]);
}
Expand All @@ -43,15 +39,21 @@ async function downloadAsFile()
transferPopupHeading.innerText = "Downloading";

/* Fetch file from MTP Device */
let fileArray = await downloadFile(device, 1, 2, progressBar);
let fileArray = await downloadFile(device, activeStorageID, activeFileID, progressBar);
console.log(typeof(fileArray));
let file_ = Uint8Array.from(fileArray);
let fileBlob = new Blob([file_]);

/* Creating URL for file object and initiating download */
let url = window.URL.createObjectURL(fileBlob);
const file_element = document.createElement("a");

let storageObject = device.storageInfoObjects.find(storageObject => storageObject.storageID === activeStorageID);
let storageIndex = device.storageInfoObjects.indexOf(storageObject);
let fileObject = device.storageInfoObjects[storageIndex].objectInfoObjects.find(fileObject => fileObject.fileID === activeFileID);

file_element.href = url;
let filename = "README.txt";
let filename = fileObject.fileName;
file_element.download = filename.split("\n")[0];
file_element.click();

Expand All @@ -68,9 +70,15 @@ async function openAsText()
transferPopupHeading.innerText = "Downloading";

let fileArray = await downloadFile(device, activeStorageID, activeFileID, progressBar);
document.getElementById("text-area").value = bin2String(fileArray);
let file_ = Uint8Array.from(fileArray);

document.getElementById("text-area").value = bin2String(file_);
document.getElementById("transferPopup").style.display = "none";

let storageObject = device.storageInfoObjects.find(storageObject => storageObject.storageID === activeStorageID);
let storageIndex = device.storageInfoObjects.indexOf(storageObject);
let fileObject = device.storageInfoObjects[storageIndex].objectInfoObjects.find(fileObject => fileObject.fileID === activeFileID);

/* Enable the user to press the close button for the popup */
transferPopupClose.style.display = "block";
transferPopupHeading.innerText = "Download Complete";
Expand All @@ -79,7 +87,11 @@ async function openAsText()
async function deleteFile()
{
await deleteObject(device, activeStorageID, activeFileID);
await getFileObjects(device, activeStorageID);

let storageObjects = await getStorageIDS(device);
let fileObjects = await getFileObjects(device, activeStorageID);

refreshUI(fileObjects, storageObjects);
}

async function closeDevice()
Expand All @@ -100,29 +112,35 @@ inputElement.oninput = function (event)
{
let arrayBuffer = reader.result;
let bytes = new Uint8Array(arrayBuffer);
uploadFile(device, 1, file, bytes, progressBar).then(() => {});
uploadFile(device, activeStorageID, file, bytes, progressBar).then(async () => {
let storageObjects = await getStorageIDS(device);
let fileObjects = await getFileObjects(device, activeStorageID);
refreshUI(fileObjects, storageObjects[0]);
});
}
}
}

function addFilesToUI(fileObjects)
{
fileManager.innerHTML = "";
for (let fileObject of fileObjects)
{
/* Create HTML elements to display File, File Size and context menu. */
let fileElement = document.createElement("div");
let fileSizeElement = document.createElement("div");
let contextMenu = document.createElement("div");

fileManager.appendChild(fileElement);
fileManager.appendChild(contextMenu);
fileManager.appendChild(fileSizeElement);

fileElement.innerHTML = fileObject.fileName;
fileElement.innerText = fileObject.fileName;
contextMenu.innerHTML = '<div class="context-menu"> <div class="item"> <i class="fa fa-download"></i> Download </div> <div class="item"> <i class="fa fa-folder-open"></i> Open as text.. </div> <div class="item"> <i class="fa fa-trash-o"></i> Delete </div> </div>';

fileManager.appendChild(fileElement);
fileElement.appendChild(contextMenu);
fileElement.appendChild(fileSizeElement);

contextMenu.id = 'context-menu-' + fileObject.fileID.toString();
fileElement.id = fileObject.fileID.toString();
fileSizeElement.className = "fileSize";

fileElement.className = "file";
activeFileID = fileObject.fileID;
Expand Down Expand Up @@ -176,6 +194,20 @@ function addFilesToUI(fileObjects)
}
contextElement.classList.add("active");
});

/* Add file size */
if (fileObject.filesize < 1024)
{
fileSizeElement.innerText = fileObject.filesize + " bytes";
}
else if (fileObject.filesize < 1048576)
{
fileSizeElement.innerText = (fileObject.filesize/1024).toFixed(2) + " KB";
}
else
{
fileSizeElement.innerText = (fileObject.filesize/1048576).toFixed(2) + " MB";
}
}
}

Expand Down
62 changes: 36 additions & 26 deletions mtp/mtp_classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class ObjectInfoDataset

/* Device class for all device operations */

class Device
class MTPDevice
{
constructor()
{
Expand All @@ -244,10 +244,19 @@ class Device
this.sessionOpen = false;
this.storageInfoObjects = new Array(0);
this.objectInfoObjects = new Array(0);
this.transactionID = 0x01;
}

getEndpoints()
async getEndpoints()
{
/* Requesting Device Descriptor */
this.device.controlTransferIn({ requestType: 'standard',
recipient: 'device',
request: 0x06,
value: 0x100,
index: 0x00},12);


this.endpointIn = 1;
this.endpointOut = 1;
}
Expand Down Expand Up @@ -300,10 +309,13 @@ class Device
try
{
this.device = await navigator.usb.requestDevice({ filters: [{}]});
await this.device.open();
await this.device.selectConfiguration(1);
await this.device.claimInterface(0);
return true;
if (this.device !== undefined)
{
await this.device.open();
await this.device.selectConfiguration(1);
await this.device.claimInterface(0);
return true;
}
}
catch
{
Expand Down Expand Up @@ -349,7 +361,7 @@ class Device
let openSession = new mtp_container(5);
openSession.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
openSession.setOperation(MTP_OPEN_SESSION);
openSession.setTransactionID(0x01);
openSession.setTransactionID(MTPDevice.transactionID++);
openSession.setParams(0, 1, 0, 0 , 0);
openSession.pack();
MTPDevice.device.transferOut(MTPDevice.endpointOut, openSession.container_array);
Expand Down Expand Up @@ -386,7 +398,7 @@ class Device
let closeSessionRequest = new mtp_container(0);
closeSessionRequest.setOperation(CLOSE_SESSION);
closeSessionRequest.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
closeSessionRequest.setTransactionID(0x06);
closeSessionRequest.setTransactionID(MTPDevice.transactionID++);
closeSessionRequest.pack()
MTPDevice.device.transferOut(MTPDevice.endpointOut, closeSessionRequest.container_array);

Expand Down Expand Up @@ -443,7 +455,7 @@ class Device
let getStorageIDS = new mtp_container(0);
getStorageIDS.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
getStorageIDS.setOperation(MTP_GET_STORAGE_IDS);
getStorageIDS.setTransactionID(0x02);
getStorageIDS.setTransactionID(MTPDevice.transactionID++);
getStorageIDS.pack();

MTPDevice.device.transferOut(MTPDevice.endpointOut, getStorageIDS.container_array);
Expand Down Expand Up @@ -490,7 +502,7 @@ class Device
})

let reqStorageInfo = new mtp_container(1);
reqStorageInfo.setTransactionID(0x09);
reqStorageInfo.setTransactionID(MTPDevice.transactionID++);
reqStorageInfo.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
reqStorageInfo.setOperation(MTP_GET_STORAGE_INFO);
reqStorageInfo.setParams(storageObject.storageID, 0, 0, 0, 0);
Expand Down Expand Up @@ -550,7 +562,7 @@ class Device

/* Get Storage IDS */
let reqObjHandles = new mtp_container(3);
reqObjHandles.setTransactionID(0x02);
reqObjHandles.setTransactionID(MTPDevice.transactionID++);
reqObjHandles.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
reqObjHandles.setOperation(GET_OBJECT_HANDLES);
reqObjHandles.setParams(storageObject, 0, GET_ROOT_OBJECTS,0, 0);
Expand Down Expand Up @@ -617,7 +629,7 @@ class Device
let getObjInfo = new mtp_container(1);
getObjInfo.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
getObjInfo.setOperation(GET_OBJECT_INFO);
getObjInfo.setTransactionID(0x03);
getObjInfo.setTransactionID(MTPDevice.transactionID++);
getObjInfo.setParams(fileObject.fileID, 0, 0, 0 , 0);
getObjInfo.pack();

Expand All @@ -633,7 +645,6 @@ class Device
{
let firstObjectBuffer = new Uint8Array(0);
let fileLength = null;
let fileBlob = null;
let objectBuffer = new Array(0);

results.then(async (receivedPackets) => {
Expand All @@ -654,22 +665,24 @@ class Device
progressBar.value = ((i/numberOfPacketsToBeReceived) * 100).toFixed(1);
})
}
resolve([true, objectBuffer]);
await MTPDevice.device.transferIn(MTPDevice.endpointIn, 512).then((result)=>{
resolve([true, objectBuffer]);
})
}
else
{
throw("Unhandled exception when initiating download.");
}
})
.catch((err) => {
console.log("Error getting file. " + err)
resolve(false);
});
// .catch((err) => {
// console.log("Error getting file. " + err)
// resolve(false);
// });
})

/* Get ObjectInfo of all received handles */
let reqObj = new mtp_container(1);
reqObj.setTransactionID(0x04);
reqObj.setTransactionID(MTPDevice.transactionID++);
reqObj.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
reqObj.setOperation(GET_OBJECT);
reqObj.setParams(fileObject.fileID, 0, 0, 0, 0);
Expand All @@ -689,10 +702,8 @@ class Device

let deletePromise = new Promise(function(resolve)
{
console.log(result);
result.then((receivedPacket) => {
resolve(true);
console.log(receivedPacket);
if ((receivedPacket[0][7] << 8 | receivedPacket[0][6]) === MTP_OK)
{
resolve("true");
Expand All @@ -707,7 +718,7 @@ class Device
})

let delete_packet = new mtp_container(1);
delete_packet.setTransactionID(0x07);
delete_packet.setTransactionID(MTPDevice.transactionID++);
delete_packet.setOperation(MTP_DELETE_OBJECT);
delete_packet.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
delete_packet.setParams(MTPDevice.storageInfoObjects[storageIndex].objectInfoObjects[fileIndex].fileID, 0, 0, 0, 0);
Expand Down Expand Up @@ -745,7 +756,7 @@ class Device
});
let sendObjInfo = new mtp_container(2);
sendObjInfo.setOperation(SEND_OBJECT_INFO);
sendObjInfo.setTransactionID(0x08);
sendObjInfo.setTransactionID(MTPDevice.transactionID++);
sendObjInfo.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
sendObjInfo.setParams(storageObject.storageID, PLACE_IN_ROOT, 0, 0, 0);
sendObjInfo.pack();
Expand All @@ -756,7 +767,7 @@ class Device

let sendObjInfo2 = new mtp_container(0);
sendObjInfo2.setOperation(SEND_OBJECT_INFO);
sendObjInfo2.setTransactionID(0x08);
sendObjInfo2.setTransactionID(MTPDevice.transactionID);
sendObjInfo2.setParams(0, 0, 0, 0, 0);
sendObjInfo2.setTransactionType(CONTAINER_TYPE_DATA);
sendObjInfo2.containerArrayLength = 12 + fileInfo.container_array.length + fileInfo.filename_array.length + fileInfo.dateModArray.length;
Expand Down Expand Up @@ -790,7 +801,7 @@ class Device

let reqSendObject = new mtp_container(0);
reqSendObject.setTransactionType(MTP_CONTAINER_TYPE_COMMAND);
reqSendObject.setTransactionID(0x10);
reqSendObject.setTransactionID(MTPDevice.transactionID++);
reqSendObject.setOperation(SEND_OBJECT);
reqSendObject.setParams(0, 0, 0, 0, 0);
reqSendObject.pack();
Expand All @@ -815,7 +826,6 @@ class Device
}
}
let sliced = fileBytes.slice(i, end);
console.log(i, end, sliced);
if (i === 0)
{
await MTPDevice.device.transferOut(MTPDevice.endpointOut, new Uint8Array([...reqSendObject.container_array, ...sliced]));
Expand Down
18 changes: 15 additions & 3 deletions mtp/mtp_operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ let device = null;
/* MTPDeviceInit() initializes the device and get a snapshot of all storage's and file objects in them */
async function MTPDeviceInit()
{
device = new Device();
device = new MTPDevice();
let status = null;
try
{
await device.connectDevice(device);
let status = await device.openSession(device);
let success = await device.getEndpoints();
let connected = false;
if (success)
{
connected = await device.connectDevice(device);
}
if (connected)
{
status = await device.openSession(device);
}

if (status === true)
{
console.log("MTP device initialized successfully.");
Expand Down Expand Up @@ -41,6 +51,7 @@ async function downloadFile(MTPDevice, storageID, fileID, progressBar)
catch(err)
{
console.log("Error downloading file. " + err);
return null;
}
}

Expand Down Expand Up @@ -120,6 +131,7 @@ async function deleteObject(MTPDevice, storageID, fileID)
let storageObject = MTPDevice.storageInfoObjects.find(storageObject => storageObject.storageID === storageID);
let storageIndex = MTPDevice.storageInfoObjects.indexOf(storageObject);
let fileObject = MTPDevice.storageInfoObjects[storageIndex].objectInfoObjects.find(fileObject => fileObject.fileID === fileID);
console.log(MTPDevice.storageInfoObjects[storageIndex].objectInfoObjects);
let status = await MTPDevice.deleteFile(MTPDevice, storageObject, fileObject);

if(status === true)
Expand Down

0 comments on commit e91ab3a

Please sign in to comment.